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

Sindhu said:   1 decade ago
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:   1 decade ago
Compiler dependent question on gcc 4.7.2 the answer is:

64 32
16 32
16 32
64 32


Post your comments here:

Your comments will be displayed after verification.