4,093 views
2 votes
2 votes
Please tell the output ! With explanation..

#include<stdio.h>

int main()
{
 printf("%d", 2<<2>>2);
    printf("\n%d", 2>>2<<2<<(1<<1));
    return 0;
}

2 Answers

2 votes
2 votes

'<<' is the bitwise left shift operator - Shifts the bits to left.

'm<<n' shifts the bits of m to the left by n times.

'>>' is the bitwise right shift operator - Shifts the bits to right.

'm>>n' shifts the bits of m to the right by n times.

Say, '2<<3': means shift bits of 2 to left 3 times

Since bit representation of 2 is 00000010

If we shift bits in 2 to left 3 times, we'll get - 00010000

Therefore, 2<<3 =  00010000 = 16

Similarly, '2>>3': means shift bits of 2 to right 3 times

2>>3 = 00000000 = 0

Associativity of bitwise left shift and right shift operators is - left to right.

The output of your program will be:

2
0


Explanation:

In the first printf, '2<<2>>'

is associated like - '((2<<2)>>2)'

Means first left shift bits in 2 by 2 and then right shift by 2, which yields 00000010, i.e 2.

In the second printf, '2>>2<<2<<(1<<1)'

is associated like - '(((2>>2)<<2)<<(1<<1))'

On evaluating this, we'll get 00000000, i.e 0.

1 votes
1 votes


Here I am breaking down your question operations working in memory.
Suppose 2 is represented in a byte somewhere in memory.So initial representations are working of shifts and finally printing the value.
In the second printf at some point all bits will be zero and thus performing any bit shift operator would result in zero. !

edited by

Related questions

4 votes
4 votes
1 answer
1
sid1221 asked Oct 14, 2017
544 views
#include <stdio.h int main() { printf("%d", 1 << 2 + 3 << 4); return 0; }some one verify answer am getting 256 , given one 512 :(
0 votes
0 votes
1 answer
3
Ravi prakash pandey asked Mar 17, 2018
921 views
Why left shift operator does't preserve its sign bit??I think not preserving sign bit is meaning less.