0

What is the output and why? Explain,,,,

#include <stdio.h > int main () { printf("%x", - 1<<1); getchar() ; return 0 ; }

7th Oct 2018, 12:10 PM
piyush damor
piyush damor - avatar
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
8th Oct 2018, 11:27 AM
Ipang