0
What is the output and why? Explain,,,,
#include <stdio.h > int main () { printf("%x", - 1<<1); getchar() ; return 0 ; }
1 Answer
+ 2
-1<<1 yields -2, but since the format specifier used "%x" (a format used for pointers, unsigned int) then the value is assumed as unsigned int. Assigning negative value to an unsigned int type means to add the negative value to the highest range of unsigned int type. Hence the value -2 is calculated as:
2^32 + (-2) â
4,294,967,296 + (-2) â
4,294,967,296 - 2 â
4,294,967,294
The use of "%x" outputs the value in hexadecimal format 0xfffffffe.
* Assuming int type size 4 bytes, 32bits.
Hth, cmiiw