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;
}
Discussion:
66 comments Page 1 of 7.
Nikhil Dhoke said:
14 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.
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.
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
But the unsigned char is 8-bit, so the overflow bit is discarded, leaving 00000000 (0).
So, I think answer is 0
Namu said:
6 years ago
How 0x80 is converted into binary?
(4)
Jasber said:
7 years ago
i=0x80 means( 0000 0000 1000 0000).
i<<1means (0000 0001 0000 0000)= 256.
i<<1means (0000 0001 0000 0000)= 256.
(6)
Keziahtabraham said:
7 years ago
0*80 means 0000 0000 1000 0000.
Then, one time left shift(i<<1):0000 0001 0000 0000
According to position 1 is at 8th position, then to convert binary to decimal, 2^8=256.
Then, one time left shift(i<<1):0000 0001 0000 0000
According to position 1 is at 8th position, then to convert binary to decimal, 2^8=256.
(3)
Rajani R said:
7 years ago
Please someone explain this solution.
ANUPA A said:
9 years ago
Please, someone clearly explain to me what does the bitwise operator<< actually does.
Vaishnavi said:
9 years ago
Kindly explain this program clearly.
Sandeep Kumar said:
9 years ago
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
In this case, also typecast operation is performed but final data is stored in 8 bit so the output is 0.
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
In this case, also typecast operation is performed but final data is stored in 8 bit so the output is 0.
(8)
Nagendra said:
9 years ago
// 1
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
Here output 256.
// 2
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
Here output 0.
* The diff is assignment of variable i.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
Here output 256.
// 2
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
Here output 0.
* The diff is assignment of variable i.
(4)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers