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.
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.
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.
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.
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.
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.
Jhalak gupta said:
1 decade ago
@sumit %d %c %f are used for printing values in int, char, float respectively and do not do any datatype conversions. Study type casting first then you ll b more clear about your concepts. +whenever any binary operators like +, -. /, * etc are used b/w different data types it. It promotes or demotes the datatype accordingly and then perform operations.
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.
Amr Gamal said:
1 decade ago
Hello All,
Simply,
unsigned char i = 0x80;
printf("%d\n", i<<1);
Produces 256 value as the output of this operation hasn't been stored back in i variable. i variable still contains 0x80 value.
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
This which will produce 0 output.
Anyone found any other thing, please tell me.
Simply,
unsigned char i = 0x80;
printf("%d\n", i<<1);
Produces 256 value as the output of this operation hasn't been stored back in i variable. i variable still contains 0x80 value.
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
This which will produce 0 output.
Anyone found any other thing, please tell me.
Davil said:
1 decade ago
What is the meaning of "return 0"?
Sachin mishra said:
1 decade ago
Here it is given unsigned char that's why we have taken 2 byte.
The hex representation of 80 is 1000 0000.
i = 0000 0000 1000 0000.
Left shift by 1.
We get 1= 0000 0001 0000 0000 = 256 as result.
The hex representation of 80 is 1000 0000.
i = 0000 0000 1000 0000.
Left shift by 1.
We get 1= 0000 0001 0000 0000 = 256 as result.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers