C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 11)
11.
What will be the output of the program?
#include<stdio.h>

int main()
{
    unsigned int res;
    res = (64 >>(2+1-2)) & (~(1<<2));
    printf("%d\n", res);
    return 0;
}
32
64
0
128
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
24 comments Page 2 of 3.

Akash said:   1 decade ago
>> means right shift....
It shifts d binary value by n places to right
Ex: 4>>2
0100>>2
1

Akash said:   1 decade ago
>> shifts binary value to right by n times

Ex: 4>>2
0000 0100 >> 2
0000 0001

Hence 4>>2 means 1

Dibyendu said:   1 decade ago
Nice vai. Thanks for the explanation.

Ranjitha said:   1 decade ago
What >> operator will do?

Karthik said:   1 decade ago
@Ranjitha

consider x>>y.
This will shift the bits of x(binary number) by y positions towards left
Ex:
1)1010>>2 = 0010
2)4>>2 (refer to exp by akash).

Revathi said:   1 decade ago
(64>>(2+1-2)) & (~(1<<2)) Becomes

(64>>1) & (~(1<<2)).

In shift left(<<) the formula is (a<<b) = a*2^b and shift right (>>) formula is (a>>b) = a/(2^b).

So,

64/(2^1) = 32 & (~(1*(2^2)) = 4, then we convert into binary,

32 = 0010 0000
4 = 0000 0100 = > ~4 = 1111 1011

Finally,

32 = 0010 0000
&~4 = 1111 1011
------------------ condition(1&1 = 1 otherwise 0)
0010 0000 = 32
------------------

Answer = 32.
(3)

Saiprabha said:   1 decade ago
In c program the precedence will be followed by "BODMAS"
(64>>(2+1-2))&((~(1<<2)))
(64>>1)&(~(2^1*1^2))
(2^6/2^1)&(~(2^3))
32&(~8)



for 32 0 0 1 0 0 0 0 0
for 8 0 0 0 0 1 0 0 0
for ~8 1 1 1 1 0 1 1 1

now 32&~8
0 0 1 0 0 0 0 0
1 1 1 1 0 1 1 1
-----------------------
0 0 1 0 0 0 0 0

output will be: 2^5=32

ANKITA said:   1 decade ago
1st 64 = 01000000;

Then <2+1-1 = 1>;

We have 2 apply right shift operator;

64>>1 so ans is 1000000>>1 = 100000 = 32;

~<1<2>=1, SO 32*1 = 32;
(2)

Saikumar said:   1 decade ago
Why is everyone is take only byte information. A number with no decimal point is considered as int so it should 2 byte long.
(1)

Rakshith said:   1 decade ago
1. #include <stdio.h>
2. void main()
3. {
4. int x = 1, y = 0, z = 5;
5. int a = x && y && z++;
6. printf("%d", z);
7. }

What's the output and how?
(1)


Post your comments here:

Your comments will be displayed after verification.