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:
65 comments Page 3 of 7.
Gopi krishna said:
1 decade ago
Size of char is 1 byte.
So it have 8 bits. The equivalent of 0*80 is 1000 0000.
If I do left shift then it is 0000 0001 so answer is 1.
This is my thinking. Can any one explain me correct?
So it have 8 bits. The equivalent of 0*80 is 1000 0000.
If I do left shift then it is 0000 0001 so answer is 1.
This is my thinking. Can any one explain me correct?
Vinay said:
1 decade ago
i = 0x80 it is in hexadecimal form.
Decimal form of i =8*16^1+0*16^0=128= 00000000 10000000 in binary form.
After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.
Decimal form of i =8*16^1+0*16^0=128= 00000000 10000000 in binary form.
After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.
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)
Sravan said:
1 decade ago
int 0x86 means it is in hexadecimal So the decimal equivalent of 86 is 128. Left shift of 128 is 256 because left shift by 1 of a number is equivalent to multiplying it with 2.
Kuljeet said:
1 decade ago
@Spa- because its an unsigned char value. its range is 0 to 255. hence it can take any value between 0 and 255, which in this case is 80, represented in hexadecimal form.
Ishu said:
1 decade ago
Try this it will be all clear.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n",(char)(i<<1));
return 0;
}
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n",(char)(i<<1));
return 0;
}
Sumit said:
1 decade ago
Nikita its not << operator , which converts char to int , its %d which makes changes with c , try to use %c it will give blank as , Ascii value of NULL is 0
Supriya said:
1 decade ago
Yes it is correct @Abhinav but when we consider that and tried to shift by 1 then answer goes wrong. Please anyone tell how I convert this hex number into binary.
Sushma said:
1 decade ago
I've the same doubt..:(
char is 1 byte
so, 0x80= 1000 0000
i<<1 = 0000 0000 rite?
Is it dat %d in the printf statement matters?
char is 1 byte
so, 0x80= 1000 0000
i<<1 = 0000 0000 rite?
Is it dat %d in the printf statement matters?
Kasi said:
2 decades ago
i = 0x80 = 00000000 10000000 in binary form.
After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.
After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers