Let's walk through your code:
short a = -5;
a = -5, which fits into a short. So far so easy.
unsigned short b = -5u;
-5u means apply the unary -
operator to the constant 5u. 5u is (unsigned int) 5, and the unary -
does no promotion, so you end up with 4294967291 which is 2^32-5. (Update: I got this bit wrong in my original answer; see a test script which shows this version is correct here http://codepad.org/hjooaQFW)
Now when putting that in b, it is truncated to an unsigned short (2 bytes, usually), so b = 65531, which is 2^16-5.
if( a == b )
In this line, a and b are both promoted to ints so that the comparison can happen correctly. If they were promoted to shorts, b would potentially wrap around. If they were promoted to unsigned shorts, a would potentially wrap around.
So it's like saying if( (int) a == (int) b )
. And a = -5, so (int) a = -5, and b = 65531, so (int) b = 65531, because ints are bigger than shorts.
a==b
expression there - is that all you're asking about? – Cosmopolitanunsigned short b = -5u
will wrap around to65531
. – Lechery