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.

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

Udit said:   6 years ago
Please explain the following in the code.
1. 32>>-0
2. 32<<-0

Animesh said:   6 years ago
I got an output 32 <<-1 = 64 and 32>>-1=16;

Am I right?

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

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

Ankita said:   6 years ago
How to convert -0 to its 2's complement?

Jason said:   7 years ago
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.

Ahmed said:   7 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)

Sai said:   7 years ago
Can any one tell me when we are taking 16 bit number & 8 bit number?

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


Post your comments here:

Your comments will be displayed after verification.