I've been experimenting with simple code and I noticed:
short x = 0x8765;
int y = (int)x;
printf("y = %d\n",y);
would print out "y = -30875". I'm wondering why is it the case since when I convert 0x8765 from hex into decimal I got y = 34661.
I've been experimenting with simple code and I noticed:
short x = 0x8765;
int y = (int)x;
printf("y = %d\n",y);
would print out "y = -30875". I'm wondering why is it the case since when I convert 0x8765 from hex into decimal I got y = 34661.
The bit pattern of 0x8765
is negative in a 16-bit signed two's complement integer, but positive in a 32-bit signed two's complement integer.
In an int16_t
:
0b1000011101100101
//^ sign bit set
In an int32_t
:
0b00000000000000001000011101100101
//^ sign bit unset
The range of an 4-bytes integer datatype with short
type qualifier in C is considered to hold the values from -32,768 to 32,767. But the way you're trying to hold the integer 34661 in the short int
is incorrect; the value couldn't be held by it.
Another point, the conversion is clearly correct, since the short int
variable x
is being overflowed, a negative integer, is then assigned to x
and it's explicitly did typecast to int
variable y
and assigned the value of x
in it.
In other words, the maximum value which could be held by short int
type in hex: 0x7FFF
(i.e. 32767).
Note: You may use unsigned short int
to extend the capacity of short
from 0 to 65535 (since it's unsigned, the value must not be the negative integer):
|(-32768) + 32767| = -(-32768) + 32767 = 32768 + 32767 => 65535
If you try to assign the number 0x8765
(34661
decimal) to a short int
you are assigning an out of range value to it. The range of short
goes from -32768
to +32767
and the literal value in decimal for 0x8765
is +34661
which is clearly over the highest value allowed for a short
. So you are incurring on Undefined Behaviour.
What happens is not specified by the standard.
It is the same problem if you try to assign 100000000000
to a char
variable. As you have not checked the value in x
you thought that the value changed at some point. But it didn't. For some unexplain reason that Undefined Behaviour resulted in -30875
being assigned to x
. But also an error could be raised by the runtime, and your program stopped, or even more strange things. That is what U.B. means. Probably if you had printed the values of x
and y
you could observe the same value on both, demonstrating that the problem came before.