C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 10)
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;
}
Garbage values
64 32
0 32
16 32
0 32
All zeros
8 0
0 0
32 0
0 16
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
42 comments Page 2 of 5.

Himani said:   9 years ago
Please, explain 32>>-1 and 32<<-1 in easy way.

Sairam said:   9 years ago
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?

Monisha said:   10 years ago
How is it possible to get 32>>1=16?

Alice said:   1 decade ago
How come 32>>1=16?

Akshay Kalra said:   1 decade ago
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.

Tej said:   1 decade ago
-1>>(any number). You get -1 not 0.

Vincent said:   1 decade ago
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.

R K said:   1 decade ago
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.
(1)

Ankush said:   1 decade ago
Please explain the answer as per GCC compiler. It is:
64 32
16 32
16 32
64 32

Ramdas said:   1 decade ago
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.


Post your comments here:

Your comments will be displayed after verification.