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 5 of 7.
Subhadeep said:
1 decade ago
What will be the answer if it was printf("%x",i<<1); ?
Raj said:
1 decade ago
Not a single explanation was useful. If anyone knows correct explanation, please help us. Thank you.
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.
Chintan said:
1 decade ago
Hello everyone in the syntax of function printf:
printf("format specifier",variables);
In which it would expect the datatype which format specifier specifies not which variable specifies.
Example: printf("%d\n",i<<0x80); will take 2 bytes because format specifier is %d instead if you write.
printf("%c\n",i<<0x80) it will take only one byte and output will be NULL.
printf("format specifier",variables);
In which it would expect the datatype which format specifier specifies not which variable specifies.
Example: printf("%d\n",i<<0x80); will take 2 bytes because format specifier is %d instead if you write.
printf("%c\n",i<<0x80) it will take only one byte and output will be NULL.
Ishu said:
1 decade ago
Try this it will be all clear.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n",(char)(i<<1));
return 0;
}
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n",(char)(i<<1));
return 0;
}
Shanthu said:
1 decade ago
Char should overflow to become zero instead its becoming 256.
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers