I'm studying signed-unsigned integer conversions and I came to these conclusions, can someone tell me if this is correct please
unsigned short var = -65537u;
Steps:
- 65537u (implicitly converted to unsigned int)
Binary representation: 0000 0000 0000 0001 0000 0000 0000 0001
- -65537u
Binary representation: 1111 1111 1111 1110 1111 1111 1111 1111
- Truncated to short
Binary representation: 1111 1111 1111 1111
- read as an unsigned short: 65535
The same should apply for the following cases:
unsigned short var = -65541u;
- 65541u (unsigned int)
0000 0000 0000 0001 0000 0000 0000 0101
- -65541u
1111 1111 1111 1110 1111 1111 1111 1011
- Truncated to short
1111 1111 1111 1011
- read as an unsigned short: 65531
unsigned short var = -5u;
- 5u (unsigned int)
0000 0000 0000 0000 0000 0000 0000 0101
- -5u
1111 1111 1111 1111 1111 1111 1111 1011
- Truncated to short
1111 1111 1111 1011
- read as an unsigned short: 65531
5
and-5
might differ only in the sign bit. – Bo Persson-5
may not be represented in 2's complement, but this is not relevant to the question, as the question does not involve any negative number. In this question, unary negation is only applied to unsigned types. For instance-65537u
is parsed as-(65537u)
. – Pascal Cuoq