C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>

int main()
{
    unsigned char i = 0x80;
    printf("%d\n", i<<1);
    return 0;
}
0
256
100
80
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
65 comments Page 4 of 7.

ANUPA A said:   8 years ago
Please, someone clearly explain to me what does the bitwise operator<< actually does.

Rajani R said:   7 years ago
Please someone explain this solution.

Mladen Saldanha said:   7 months ago
if were a larger type (e.g., int), 0x80 << 1 would be 256 (binary 100000000).
But the unsigned char is 8-bit, so the overflow bit is discarded, leaving 00000000 (0).
So, I think answer is 0

Shanthi said:   1 decade ago
How would you convert binary into decimal (0000 0001 0000 0000=256) ?

Kasi said:   2 decades ago
i = 0x80 = 00000000 10000000 in binary form.

After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.

Madureddy said:   1 decade ago
Yes Kasi is correct.

Shruti said:   1 decade ago
What is 0x80?

How do we conver it to binary?

Tomek said:   1 decade ago
I got caught because I thought there would be overflow and it becomes 0. But char is converted to int before <<

Raj said:   1 decade ago
0x80 is hex representation and its binary equivalent is 1000 0000

Yachana said:   1 decade ago
Why you are taking 2 bytes instead of 1 byte.


Post your comments here:

Your comments will be displayed after verification.