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 4 of 7.
Satish said:
1 decade ago
First We have to convert hexadecimal to Decimal.
Then we have to find the binary value of that.
Then we have to find the binary value of that.
RajaMohan said:
1 decade ago
Step 1 :
First convert the Hexadecimal into decimal
0x80 ---> 16*8=128.
Step 2 :
Then use bit-wise operation left shift.
128*pow(2,1).
Here 1 indicates the value after left shift.
First convert the Hexadecimal into decimal
0x80 ---> 16*8=128.
Step 2 :
Then use bit-wise operation left shift.
128*pow(2,1).
Here 1 indicates the value after left shift.
Kundan Kumar said:
1 decade ago
First you have look what is value of i.
Value of i is 0x80
It means value in Hexadecimal.
So,
Write it's binary in four bit for each digit
For zero(0) 0000.
For eight(8) 1000.
Finally you get this binary in 16-bit 0000 0000 10000 0000.
Now solve this expression i<<1.
Shift one(1) bit after shifting you get this binary
0000 0001 0000 0000 and print it %d(decimal value) i.e 256.
Value of i is 0x80
It means value in Hexadecimal.
So,
Write it's binary in four bit for each digit
For zero(0) 0000.
For eight(8) 1000.
Finally you get this binary in 16-bit 0000 0000 10000 0000.
Now solve this expression i<<1.
Shift one(1) bit after shifting you get this binary
0000 0001 0000 0000 and print it %d(decimal value) i.e 256.
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.
Mayur said:
1 decade ago
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
i=0x80 means 0000 1000 0000
After <<(1 bit shift left) It will become
0001 0000 0000 that means 256.
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
i=0x80 means 0000 1000 0000
After <<(1 bit shift left) It will become
0001 0000 0000 that means 256.
ANJANI said:
1 decade ago
Thanks a lot. For your nice explanation. I m fully overwhelm.
Chandini said:
1 decade ago
Please explain briefly about this program.
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.
Spa said:
1 decade ago
Why everyone taking for char 2bytes. ? can any one explain me.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers