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 2 of 7.
Sunil said:
1 decade ago
Hello all,
We are representing 128 in 2 byte because of the followings :
Case 1 - sizeof i=1 \\i is of character data type here.
Case 2 - sizeof 0x80=2 \\ (for 16 bit OS).
Case 3 - sizeof 128=2 \\(for 16 bit OS).
Case 4 - sizeof 'A'=2 \\unsigned char i = 'A';
Case 4 also requires 2 byte to be stored in the memory. So all the R-value(constants) of character take 2 byte from the memory.
Character constants like '0x80' are stored in memory in their corresponding ASCII value,as ASCII values are integer they requires 2 byte of memory.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",sizeof 0x80);
printf("%d\n",sizeof i);
return 0;
}
Output
256
2
1
Thanks.
We are representing 128 in 2 byte because of the followings :
Case 1 - sizeof i=1 \\i is of character data type here.
Case 2 - sizeof 0x80=2 \\ (for 16 bit OS).
Case 3 - sizeof 128=2 \\(for 16 bit OS).
Case 4 - sizeof 'A'=2 \\unsigned char i = 'A';
Case 4 also requires 2 byte to be stored in the memory. So all the R-value(constants) of character take 2 byte from the memory.
Character constants like '0x80' are stored in memory in their corresponding ASCII value,as ASCII values are integer they requires 2 byte of memory.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",sizeof 0x80);
printf("%d\n",sizeof i);
return 0;
}
Output
256
2
1
Thanks.
Raj said:
1 decade ago
Not a single explanation was useful. If anyone knows correct explanation, please help us. Thank you.
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.
Subhadeep said:
1 decade ago
What will be the answer if it was printf("%x",i<<1); ?
Davil said:
1 decade ago
What is the meaning of "return 0"?
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.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers