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.
Chandan Kumar said:
10 years ago
@Abhinav.
No, its not correct. 0X80 is a hexadecimal number, and for hexadecimal number you have to convert nibble by nibble.
For example:
0X80 --------> two nibbles 8 0.
1000 0000.
0X 75 -------> 7 5.
0111 0101.
Although, you are right if it were decimal number.
For example:
80 ----------> 01010000.
"0X" makes it hexadecimal.
No, its not correct. 0X80 is a hexadecimal number, and for hexadecimal number you have to convert nibble by nibble.
For example:
0X80 --------> two nibbles 8 0.
1000 0000.
0X 75 -------> 7 5.
0111 0101.
Although, you are right if it were decimal number.
For example:
80 ----------> 01010000.
"0X" makes it hexadecimal.
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)
Jay said:
1 decade ago
#include<stdio.h>
int main()
{
char i=0x04;
char k=0x80;
printf("%d",i<<6);
printf("%d",k<<1);
return 0;
}
Output of first print statement is 256 while for other it is -256 how?
int main()
{
char i=0x04;
char k=0x80;
printf("%d",i<<6);
printf("%d",k<<1);
return 0;
}
Output of first print statement is 256 while for other it is -256 how?
Sandeep Kumar said:
8 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)
Adam said:
10 years ago
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
int a=i<<1;
unsigned long b=a;
unsigned char c=b;
printf("%d\n",c);
b=b>>1;
c=b;
printf("%d\n",c);
return 0;
}
Try this.
int main()
{
unsigned char i = 0x80;
int a=i<<1;
unsigned long b=a;
unsigned char c=b;
printf("%d\n",c);
b=b>>1;
c=b;
printf("%d\n",c);
return 0;
}
Try this.
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.
Sutendra mirajkar said:
1 decade ago
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d %d %d\n", i);
return 0;
}
Then the output of i is zero can anyone explain why is it so?
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d %d %d\n", i);
return 0;
}
Then the output of i is zero can anyone explain why is it so?
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.
Mladen Saldanha said:
7 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
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