What number does each line of code print out Bridfly explain
What number does each line of code print out? Bridfly explain why.
printf(\"%d\ \", ( 0x40000000 << 1) >> 20);
printf(\"%d\ \", ( 0x40000000u << 1) >>20);
Solution
#include <stdio.h>
int main()
 {
 printf(\"%d\ \",0x40000000);
 //Left shift 1 multiplies the number by 2. Result : 2147483648. Max value of int is 2147483647.
 //Overflow changes sign to -2147483648.
 printf(\"%d\ \",(0x40000000 << 1));
 //Right shift by 1 divides the number by 2^1. So right shift by 20 will divide -2147483648 by 2^20. Result: -2048
 printf(\"%d\ \", ( 0x40000000 << 1) >> 20);
   
 //Below result will print 2048 because appending u at the end of constant makes it unsigned int. So sign will be ignored for this case.
 printf(\"%d\ \", ( 0x40000000u << 1) >>20);
 return 0;
 }

