简体   繁体   中英

Borland C/C++ cast short to unsigned int

How does borland cast short to unsigned int ?

#include <stdio.h>

void main() {
  short sNum = 57000;
  unsigned short usNum = sNum;
  unsigned int uiNum;

  printf("\r\nsNum = %d", sNum);
  printf("\r\nusNum = %u", usNum);

  //cast 1: short -> unsigned int
  uiNum = sNum;
  printf("\r\nuiNum = %u", uiNum);

  //cast 2: unsigned short -> unsigned int
  uiNum = (unsigned short)sNum;
  printf("\r\nuiNum = %u", uiNum);
}

gives me the output:

sNum = -8536
usNum = 57000
uiNum = 4294958760
uiNum = 57000

Why i dont get 57000 in cast 1: short to unsigned int (is there no implicit cast to unsigned short before the cast to unsigned int ?) ?

There is only one cast in your code:

uiNum = (unsigned short)sNum;

The rest of the things you're calling casts are called (implicit) conversions in the C language. There are no hidden intermediate steps in conversions. If you want to go through an intermediate type, you can force it via a cast or assignment into a variable of that type.

Secondly, the line:

short sNum = 57000;

has implementation-defined behavior. You should avoid code which converts (implicitly or via a cast) values to signed types into which the value does not fit.

Finally, you should not ask a question like this about "C/C++". There is no such language, and the type systems (even for plain integer types) are sufficiently different in the two languages that many questions will have different answers depending on which language you're actually using.

No, there is no implicit cast to unsigned short before the cast to unsigned int. Why would there be?

First of all, if shorts are 16 bits in this implementation, 57000 won't fit, so you can't store 57000 in your short sNum. It will overflow, yielding -8536.

In cast 2, you first convert (using an explicit cast) from short (-8536) to unsigned short (57000, since that is the same bit pattern as -8536, but interpreted as an unsigned number), and then from unsigned short to unsigned int (57000).

In cast 1, you convert directly from short (-8536) to unsigned int.

Because you asked to print a signed integer with your

 printf("\r\nsNum = %d", sNum);

line. The variable was sign extended to an int and passed as an int to the printf function, which interpreted that parameter as an int.

If you did

printf("\r\nsNum = %u", sNum);

you would get 57000

You are putting into sNum a value outside of a short's range. What you get stored is the integer -8536, which converted to an int and then to an unsigned int is 4294958760.

You need to either declare sNum as an unsigned short (so it can store the value you want) or convert it to an int instead of an unsigned int.

short has 8*2 = 16 bits

int has 8*4 = 32 bits

Unless mentioned explicitely as unsigned both int and short are signed . In case of an signed number the left most bit holds the sign bit.

so in signed short we are left with 16 - 1 = 15 bits . Hence the upper limit of short should be 2^15 32768. for any number greater than this it is deducted from the 2^16 ( = 65536 upper limit for unsigned or the total capacity ) and stored with a negative sign [ this is the ones compliment ]

Here 65536 - 57000 = -8536 Hence the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM