C Programming - Bitwise Operators - Discussion

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;
}

[A]. 0
[B]. 256
[C]. 100
[D]. 80

Answer: Option B

Explanation:

No answer description available for this question.

Kee said: (Sep 1, 2010)  
Pleases give the step.

Kasi said: (Sep 2, 2010)  
i = 0x80 = 00000000 10000000 in binary form.

After i<<1 it becomes 00000001 00000000. Its decimal equivallent is 256.

Madureddy said: (Feb 5, 2011)  
Yes Kasi is correct.

Tomek said: (Feb 19, 2011)  
I got caught because I thought there would be overflow and it becomes 0. But char is converted to int before <<

Shruti said: (Feb 19, 2011)  
What is 0x80?

How do we conver it to binary?

Raj said: (Feb 24, 2011)  
0x80 is hex representation and its binary equivalent is 1000 0000

Yachana said: (Apr 8, 2011)  
Why you are taking 2 bytes instead of 1 byte.

Srinivas said: (Apr 12, 2011)  
Good.Nice Explanation Without having any doubts.Thank you

Rashmi said: (May 10, 2011)  
Yeah, how can we take two bytes for a character.

Swati said: (May 17, 2011)  
0x88 means 128 in decimal hence
0000 0000 1000 0000
now left shift 1 byte means
0000 0001 0000 0000=256

Divya S said: (May 26, 2011)  
Nice explanation kasi. Thanks.

Swathi said: (May 31, 2011)  
How to convert 0x88 int obinary can any one explain please ?

Apurva Nigam said: (Jun 29, 2011)  
@Swathi:
to conevrt hex to binary u need to take each digit of hex value and write its binary equivalent.
For eg:-
if hex = 88 , its binary would be
bin = 1000 1000
since binary equivalent of 8(decimal) is 1000.

example2:-
if hex = AF , its binary equi is
bin = 1010 1111
as binary equi of 'A'(in decimal system its 10) 1010 and that of 'F'(in decimal its 15) is 1111

Hope this will help u.
Take care :)

Nikita said: (Jul 2, 2011)  
char converted to int before <<

What happens if float or double is there?

Siva said: (Jul 11, 2011)  
Nice explination apurva.

Sunil Sharma,Kaverinagar said: (Jul 26, 2011)  
What do you mean by 0*80?

Shanthi said: (Sep 8, 2011)  
How would you convert binary into decimal (0000 0001 0000 0000=256) ?

Ullas K said: (Sep 14, 2011)  
@nikita.

Bitwise operator can't be applied to float and double type. This its limitation,

Check out in books.

Kunal said: (Sep 18, 2011)  
What is the binary value of x (hexadecimal no) ?

Mayuri said: (Dec 16, 2011)  
Char is 1byte in size. Then why everyone having explanation assuming it is 2 byte ?

Sushma said: (Dec 16, 2011)  
I've the same doubt..:(

char is 1 byte
so, 0x80= 1000 0000
i<<1 = 0000 0000 rite?

Is it dat %d in the printf statement matters?

Ravikant said: (Dec 29, 2011)  
Bitwise operators operate on always nos. Means int, so it converted into int.

Mehul said: (Jan 17, 2012)  
@Apurva Nigam Thanks a lot.

Sutendra Mirajkar said: (Jan 18, 2012)  
#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?

Chotu said: (Mar 8, 2012)  
Dear sutendra Mirajkar

int main()
{
unsigned char i = 0x80;
i=i<<1;// i is char so 0.
0x80 in binary 1000 0000
after letf shift(i<<1) is 0000 0000 .That value store in i char(1byte 00000000)
printf("%d\n", i);// out put is 0
return 0;
}
//////////////////////////////////////

our result in inter thats y i take int var; i<<1 value is store int var. The output is 256
int main()
{
unsigned char i = 0x80;
int var;
var=i<<1;
printf("%d %d %d\n", var);
return 0;
}

Sumit said: (Jul 7, 2012)  
Nikita its not << operator , which converts char to int , its %d which makes changes with c , try to use %c it will give blank as , Ascii value of NULL is 0

Spa said: (Aug 18, 2012)  
Why everyone taking for char 2bytes. ? can any one explain me.

Kuljeet said: (Aug 27, 2012)  
@Spa- because its an unsigned char value. its range is 0 to 255. hence it can take any value between 0 and 255, which in this case is 80, represented in hexadecimal form.

Chandini said: (Aug 27, 2012)  
Please explain briefly about this program.

Anjani said: (Sep 1, 2012)  
Thanks a lot. For your nice explanation. I m fully overwhelm.

Mayur said: (Nov 15, 2012)  
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.

Vinay said: (Nov 27, 2012)  
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.

Kundan Kumar said: (Nov 28, 2012)  
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.

Rajamohan said: (Dec 10, 2012)  
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.

Satish said: (Dec 18, 2012)  
First We have to convert hexadecimal to Decimal.
Then we have to find the binary value of that.

Jhalak Gupta said: (Mar 22, 2013)  
@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: (Mar 22, 2013)  
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: (Apr 30, 2013)  
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.

Davil said: (Jul 11, 2013)  
What is the meaning of "return 0"?

Sachin Mishra said: (Aug 8, 2013)  
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.

Subhadeep said: (Aug 9, 2013)  
What will be the answer if it was printf("%x",i<<1); ?

Raj said: (Aug 26, 2013)  
Not a single explanation was useful. If anyone knows correct explanation, please help us. Thank you.

Sunil said: (Dec 31, 2013)  
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.

Chintan said: (May 2, 2014)  
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.

Ishu said: (Jun 20, 2014)  
Try this it will be all clear.

#include<stdio.h>

int main()
{
unsigned char i = 0x80;
printf("%d\n",(char)(i<<1));
return 0;
}

Shanthu said: (Jul 17, 2014)  
Char should overflow to become zero instead its becoming 256.

Gopi Krishna said: (Sep 14, 2014)  
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?

Jay said: (Oct 4, 2014)  
#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?

Parveen Rohaj said: (Nov 9, 2014)  
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.

Abhinav said: (Jul 17, 2015)  
Here x080 can be written as 0000 0000 0101 0000. Is it true?

Supriya said: (Jul 27, 2015)  
Yes it is correct @Abhinav but when we consider that and tried to shift by 1 then answer goes wrong. Please anyone tell how I convert this hex number into binary.

Chandan Kumar said: (Feb 25, 2016)  
@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.

Adam said: (Mar 3, 2016)  
#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.

Nagarjun said: (Mar 24, 2016)  
The answer should be zero. I checked it. Unsigned char takes only single byte.

Mayur said: (Aug 29, 2016)  
Answer to Why everyone taking for char 2bytes?

we are not taking char as 2 bytes.
Let's picture the real scenario, the memory stored in the register of RAM.
for example, suppose 'i' is stored at 0x00 as shown in the below:

Address values
0x06 0000 0000
0x05 0000 0000
0x04 0000 0000
0x03 0000 0000
0x02 0000 0000
0x01 0000 0000
0x00 1000 0000 <----- i = 0x80;

now if we right shift the value of 'i', then MSB(most significant bit) will shifted to the next register* as shown below,

Address values
0x06 0000 0000
0x05 0000 0000
0x04 0000 0000
0x03 0000 0000
0x02 0000 0000
0x01 0000 0001
0x00 0000 0000 <----- i = 0x00;

It should show 0 if we read the value of 'i' as a character(%C) which is NULL(it will print NULL which is not a visible character like space).
But here we are reading the value of 'i' as integer(%d) so it will read the 4 bytes, which are,

0x03 0000 0000
0x02 0000 0000
0x01 0000 0001
0x00 0000 0000

In single line 00000000 00000000 00000001 00000000 <----- i = 0x100 = 256.

@Gopi Krishna, If we right shift the variable then MSB goes to next register's LSB, not the same one in C language.

One you are describing is the case of the microcontroller/processes @Kunal.

Gopi.V said: (Nov 13, 2016)  
if char i=0,if we use %c than it should print 0 on stdout.

But if we use %d it should print the equal ASCII value of that char.if it is 0 than 48 should print.memory will take on datatype but on formatspecifier provided.any way char should take 1byte to store,for signed char the range from -128 to +127.1 bit used for sign representation. For unsign char no loss of bit ranges from 0 to 255.256 not valid in char type.
in microcontroller suppouse 8051 all registers 8-bit othar dptr(16bit) if accumulator a regester value exceeds 8 bit then it will generate the carry.

Nagendra said: (Jan 21, 2017)  
// 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.

Sandeep Kumar said: (Jun 8, 2017)  
#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.

Vaishnavi said: (Jun 19, 2017)  
Kindly explain this program clearly.

Anupa A said: (Jun 27, 2017)  
Please, someone clearly explain to me what does the bitwise operator<< actually does.

Rajani R said: (Aug 28, 2018)  
Please someone explain this solution.

Keziahtabraham said: (Dec 14, 2018)  
0*80 means 0000 0000 1000 0000.

Then, one time left shift(i<<1):0000 0001 0000 0000
According to position 1 is at 8th position, then to convert binary to decimal, 2^8=256.

Jasber said: (Jul 10, 2019)  
i=0x80 means( 0000 0000 1000 0000).

i<<1means (0000 0001 0000 0000)= 256.

Namu said: (Nov 7, 2019)  
How 0x80 is converted into binary?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.