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:
66 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?
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?
Parveen Rohaj said:
1 decade ago
Some are having doubt that why everyone having explanation assuming it is 2 byte?
First clear in you mind that we are not converting 1 byte of character into 2 byte.
Because if we do this then we have to write i = i<<2; but we are just printing a integer value using integer format specifier, but not changing the value of i.
After this program try this.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",i);
return 0;
}
I am just want to show that value of i is not changing.
You will get the output as:
256 as value of integer after left shift and 128 as the value of the i.
So don't confuse between both.
First clear in you mind that we are not converting 1 byte of character into 2 byte.
Because if we do this then we have to write i = i<<2; but we are just printing a integer value using integer format specifier, but not changing the value of i.
After this program try this.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",i);
return 0;
}
I am just want to show that value of i is not changing.
You will get the output as:
256 as value of integer after left shift and 128 as the value of the i.
So don't confuse between both.
Abhinav said:
1 decade ago
Here x080 can be written as 0000 0000 0101 0000. Is it true?
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.
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.
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.
Nagarjun said:
10 years ago
The answer should be zero. I checked it. Unsigned char takes only single byte.
Vaishnavi said:
9 years ago
Kindly explain this program clearly.
ANUPA A said:
9 years ago
Please, someone clearly explain to me what does the bitwise operator<< actually does.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers