Now Pavan Manjunat's remote answer gave the correct answer for one case, assuming that int as usual a 32-bit type. Integer constant
0xffffffff
has a value of 2^32 - 1 and cannot be represented using int , but it is represented as unsigned int . Thus, its type is unsigned int (6.4.4.1). Therefore, x converted to unsigned int to be added, and
((~(x+0xffffffff))>>n)
estimated as
((~(0x80000000u + 0xffffffffu)) >> n) ((~0x7fffffffu) >> n) (0x80000000u >> n)
with a value of 2^(31-n) if 0 <= n < 32 (this is undefined behavior if n is outside this range).
Otherwise, ouah's answer is correct when x = 0x80000000 is int , ~0x8000000 = 0x7fffffff = INT_MAX and INT_MAX + 1 is undefined behavior in the form of integer overflow.
However, the general behavior is a wrapper, and then the result of the addition is an integer with the sign 0x80000000 , and the shift to the right of negative integers is the behavior determined by the implementation (6.5.7). Common shifts with the extension sign, which will give the result -2^(31-n) , which is then interpreted as unsigned int with the value 2^32 - 2^(31-n) the printf %x conversion specifier.
Daniel Fischer
source share