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:
66 comments Page 4 of 7.

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

Mladen Saldanha said:   11 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

Nikhil Dhoke said:   18 hours ago
It's pretty simple.

Step1 ) it's 8 0, binary is 1000 0000 <= 128.
8 0
Step2) char is converted to integer (It becomes int first, then the shift happens).
Step 3) Any type smaller than int (like char, short) is automatically promoted to int before arithmetic operations.
Step 4) if 1000 000 << 1.
Step 5) it becomes 0001 0000 0000.
Step 6) which is 256.

Rashmi said:   1 decade ago
Yeah, how can we take two bytes for a character.

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

Sunil sharma,kaverinagar said:   1 decade ago
What do you mean by 0*80?

Siva said:   1 decade ago
Nice explination apurva.

Nikita said:   1 decade ago
char converted to int before <<

What happens if float or double is there?

Apurva Nigam said:   1 decade ago
@Swathi:
to conevrt hex to binary u need to take each digit of hex value and write its binary equivalent.
For eg:-
if hex = 88 , its binary would be
bin = 1000 1000
since binary equivalent of 8(decimal) is 1000.

example2:-
if hex = AF , its binary equi is
bin = 1010 1111
as binary equi of 'A'(in decimal system its 10) 1010 and that of 'F'(in decimal its 15) is 1111

Hope this will help u.
Take care :)

Swathi said:   1 decade ago
How to convert 0x88 int obinary can any one explain please ?


Post your comments here:

Your comments will be displayed after verification.