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.

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.

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.

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

NJ Nath said:   6 years ago
32>>-1 and 32<<-1 are compiler dependent questions, in gcc compiler both values are showing as 16 and 62 respectively.

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?

Md Wasim Akram said:   7 years ago
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.

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

Kasi said:   1 decade ago
I need bief explanation of >>, << for these two operartors with an example.
Can any one help me.

Kgp vinod said:   1 decade ago
Ravishankara Asakapalli :
0xfffff this is hexadecimal number.
where f=1111
so ffff=1111 1111 1111 1111

Laxman said:   7 years ago
-1 times shifting is exactly equal to 31 times according to gcc when given variable is intiger type.


Post your comments here:

Your comments will be displayed after verification.