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 1 of 5.

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)

Ahmed said:   8 years 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.

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

Venkatesh said:   8 years ago
32<<-1 is equal to 32<<FFFF.

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.

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

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.

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

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.

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

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


Post your comments here:

Your comments will be displayed after verification.