C Programming - Bitwise Operators - Discussion

10. 

What will be the output of the program?

#include<stdio.h>

int main()
{
    printf("%d %d\n", 32<<1, 32<<0);
    printf("%d %d\n", 32<<-1, 32<<-0);
    printf("%d %d\n", 32>>1, 32>>0);
    printf("%d %d\n", 32>>-1, 32>>-0);
    return 0;
}

[A]. Garbage values
[B]. 64 32
0 32
16 32
0 32
[C]. All zeros
[D]. 8 0
0 0
32 0
0 16

Answer: Option B

Explanation:

No answer description available for this question.

Sangharsh said: (Sep 28, 2010)  
How is 32 << -1 is equal to 0 ?

Madhureddy said: (Feb 5, 2011)  
Hi friends, can any body explain it to me I'm helpless ?

Sundar said: (Feb 5, 2011)  
Refer the explanation given following questions. You will understand it better way.

http://www.indiabix.com/c-programming/bitwise-operators/discussion-511

http://www.indiabix.com/c-programming/bitwise-operators/discussion-503

Hope this help you. Have a nice day!

Atul said: (Mar 2, 2011)  
32 << -1 is nothing but 32 << 256 (2's complement of -1 in decimal notation) which will add 0 to all bit positions of 32.

Regi said: (Mar 4, 2011)  
Can any one explain clearly?

Apurva Nigam said: (Jun 29, 2011)  
Thanks Atul.

Nikita said: (Jul 2, 2011)  
2's comp of -1 is 1, how 256?

Rashmi Mate said: (Jul 5, 2011)  
How is 32 << -1 is equal to 0 ? I'm unable to understand this.

Raj Naik said: (Jul 7, 2011)  
@Nikita

Negative numbers are treated with 2's complement method.

1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2's complement: Adding 1 to the result of 1's complement

Hence

1111 1110 +
0000 0001
___________________
1111 1111 --> Which is equal to 256

Nitesh said: (Aug 7, 2011)  
Ok so -1 is being treated here as 256. Thanx Atul.

Vineet said: (Sep 19, 2011)  
Hello,

Why are you all taking -1 as 8 bit number? I can't understand because by default every integer constant is of 16 bits.

Divya said: (Oct 29, 2011)  
@vineet:

You are right. We have to consider 16 bits then -1 is represented as 16 '1's in 2's compliment. So it equals to (2 power 16)-1 if you shift by that many times the value of the exprssion is 0.

Kumar said: (Mar 26, 2012)  
How can 1111 1111 be 256, its 255 no? if I am wrong please tell me how it is?

Kumar said: (Jun 1, 2012)  
32<<-1 is similar to -1>>32.Try this you will get answer 0 for that

Kasi said: (Jun 5, 2012)  
I need bief explanation of >>, << for these two operartors with an example.
Can any one help me.

Ravishankara Asakapalli said: (Sep 7, 2012)  
I need a complete Explanation on the 0xfffff. i didn't get what exactly it means.............

Kgp Vinod said: (Oct 13, 2012)  
Ravishankara Asakapalli :
0xfffff this is hexadecimal number.
where f=1111
so ffff=1111 1111 1111 1111

Shikha said: (Jan 27, 2013)  
For an instance 32<<1
32 can be written as : 0011 0010
When you shift left by 1 bit, you get : 0110 0100
Which is 64. Similarly you can try for all options.

Vasani Anil said: (Apr 13, 2013)  
printf("%d %d\n", 32<<1, 32<<0); // 64 32 same as operation.

printf("%d %d\n", 32<<-1, 32<<-0);

-1=11111111 11111111 it's value more than 16 so o/p 0 32.

printf("%d %d\n", 32>>1, 32>>0);

printf("%d %d\n", 32>>-1, 32>>-0);// same as above reason.

Sindhu said: (Jul 6, 2013)  
32<<1 means : Binary value of 32 is 00100000 << 1 gives 01000000 gives 64.

32<<0 has no change gives the same value.

32<<-1 means: As -1 is negative number should take its 2's compliment value i.e 15 .so << by 15bits gives result zero to binary value of 32.

Saikumar said: (Aug 21, 2013)  
Compiler dependent question on gcc 4.7.2 the answer is:

64 32
16 32
16 32
64 32

Siva said: (Dec 7, 2013)  
Tell me the value of 60 and ~60 same how? explain now.

Ramdas said: (May 12, 2014)  
Its compiler dependent or we can say if there is negative No. At right hand side of >> or <<. Then the answer of that expression will be undefined.

Ankush said: (Jun 15, 2014)  
Please explain the answer as per GCC compiler. It is:
64 32
16 32
16 32
64 32

R K said: (Dec 28, 2014)  
In case:

32<<1 means 0010 0000 are add +1 0100 0000 = 64.

32<<0 means 0010 0000 are add +0 0010 0000 = 32.

32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.

32<<-0 means 0010 0000 L less -0 0010 0000 = 32.

32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.

32>>-0 means 0010 0000 L less -0 0010 0000 = 0.

Vincent said: (Dec 31, 2014)  
C reference manual says that result of a shift by negative value is undefined. So it really means probably that this value depends on a C compiler. Because of this I do not think that the question is appropriate for general C language test.

Tej said: (Jan 24, 2015)  
-1>>(any number). You get -1 not 0.

Akshay Kalra said: (Feb 22, 2015)  
For gcc compiler Answer is:

64 32.
16 32.
16 32.
64 32.

Because 32 << -1 is equivalent to 32 >> 1 == 16 and I guess rest is fine.

Alice said: (Jun 17, 2015)  
How come 32>>1=16?

Monisha said: (Mar 30, 2016)  
How is it possible to get 32>>1=16?

Sairam said: (Oct 31, 2016)  
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf(&quot;%d&quot;, d);
}

Can anyone say answer for this?

Himani said: (Dec 10, 2016)  
Please, explain 32>>-1 and 32<<-1 in easy way.

Venkatesh said: (Jul 25, 2017)  
32<<-1 is equal to 32<<FFFF.

Sai said: (Oct 3, 2017)  
Can any one tell me when we are taking 16 bit number & 8 bit number?

Ahmed said: (Dec 19, 2017)  
In case;

32<<1 means 0010 0000 are add +1 0100 0000 = 64.
32<<0 means 0010 0000 are add +0 0010 0000 = 32.
32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.
32<<-0 means 0010 0000 L less -0 0010 0000 = 32.
32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.
32>>-0 means 0010 0000 L less -0 0010 0000 = 0.

What the mean of the abbreviation l Less -0 and Zero -1.

Jason said: (Dec 30, 2017)  
The behavior is undefined according to "standard" C. You may interpret -1 as shift into opposite direction ( << -1 is actually >> 1) or you may interpret as (unsigned ) -1.

Ankita said: (Aug 5, 2018)  
How to convert -0 to its 2's complement?

Laxman said: (Jan 23, 2019)  
-1 times shifting is exactly equal to 31 times according to gcc when given variable is intiger type.

Md Wasim Akram said: (Jan 29, 2019)  
In Codeblock GCC Compiler 32 Bit its o/p is;

64 32
16 32
16 32
64 32

But unfortunately, this answer is not available in option.

Animesh said: (Mar 26, 2019)  
I got an output 32 <<-1 = 64 and 32>>-1=16;

Am I right?

Udit said: (Jun 4, 2019)  
Please explain the following in the code.
1. 32>>-0
2. 32<<-0

Nj Nath said: (Mar 29, 2020)  
32>>-1 and 32<<-1 are compiler dependent questions, in gcc compiler both values are showing as 16 and 62 respectively.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.