Machine-translated by Google. Questions are authored in English.
Top

Discussion

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;
}

 

  • A.32
  • B.64
  • C.128

Answer: A

Correct answer: 32

Take the two halves separately. The shift count evaluates first: \( 2+1-2 = 1 \), so the left operand is \( 64 \gg 1 = 32 \).

The right operand is ~(1<<2), a mask with every bit set except bit 2 - the clear-bit-2 mask.

Now \( 32 = 100000_2 \) has its only bit in position 5, which the mask leaves alone, so the AND changes nothing:

\[ 32 \mathbin{\&} \mathtt{\sim}4 = 32 \]

The masking is a decoy: it would have mattered only if the value had bit 2 set.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.