Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned-signed underflow mechanism

Tags:

c++

c

I know that the following

unsigned short b=-5u;

evaluates to b being 65531 due to an underflow, but I don't understand if 5u is converted to a signed int before being transformed into -5 and then re-converted back to unsigned to be stored in b or -5u is equal to 0 - 5u (this should not be the case, -x is a unary operator)

like image 437
Johnny Pauling Avatar asked Jan 14 '23 01:01

Johnny Pauling


1 Answers

5u is a literal unsigned integer, -5u is its negation.. Negation for unsigned integers is defined as subtraction from 2**n, which gets the same result as wrapping the result of subtraction from zero.

like image 197
jthill Avatar answered Jan 21 '23 13:01

jthill