At IndiaBix.com, I came across the following question. According to my experience level (new to C), the output above should be 0 (10000000 << 1 is 00000000), but it turned out to be 256, after I went deeper, I found that we print using % d , which supports 4 bytes, so the output is 256 instead of 0.
#include<stdio.h>
int main()
{
unsigned char i = 128;
printf("%d \n", i << 1);
return 0;
}
Now consider the following example
#include<stdio.h>
int main()
{
unsigned int i = 2147483648;(bit 31 = 1 and b0 to b30 are 0)
printf("%d \n", i<<1);
return 0;
}
When I left the shift above, I get 0 as output, As% d supports int, the output should be 0, but when I changed% d to% ld, the result is still 0. Since% ld supports values longer than int output should be 0. Why am I getting 0 as output.
source
share