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 1 of 3.

Jabya said:   1 year ago
Shifting operations done only on positive numbers.

Harsh said:   4 years ago
@Datta is right.
(1)

Parveen said:   9 years ago
According to my point of view,

32 is represented as 00000000 00000000 00000000 00100000 for gcc compiler and for c compiler 00000000 00100000.

And 4 is represented as 00000000 00000000 00000000 00000100 for gcc compiler and for c compiler 00000000 00000100.

So when we use ~ (4) it will be like.

For gcc compiler 11111111 11111111 11111111 11111011

For turboc compiler 11111111 11111011

So when we apply & operation then for gcc compiler.

00000000 00000000 00000000 00100000

& 11111111 11111111 11111111 11111011
-----------------------------------------

00000000 00000000 00000000 00100000

-----------------------------------------

Which is equal to 32?

For turbo c compiler.

00000000 00100000

& 11111111 11111011

---------------------------------------

00000000 00100000

---------------------------------------

Which is equal to 32?
(1)

Ashu bhosale said:   10 years ago
@Rakshith.

Answer is z=5.

Explaination:

a= x&&y&&z++;
1&&0&&5++;
0&&5++;
But the logical operators && and || never checks the right side expression if lhs are 0 & 1 respectively.

So && in 0&&5++ doesn't check 5++ condition.

So z=5.

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?

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.

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;

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

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.
(1)

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).


Post your comments here:

Your comments will be displayed after verification.