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

Discussion

Predict the output of following program.

#include <stdio.h>

int main()
{
 char flag=0x0f;

 flag &= ~0x02;
 printf("%d",flag);

 return 0;
}

 

  • A.13
  • B.d
  • C.22
  • D.10

Answer: A

Correct answer: 13

Work through the mask. 0x0f is \( 1111_2 \) and 0x02 is \( 0010_2 \), so ~0x02 is ...11111101 - every bit set except bit 1.

AND then clears exactly that bit and leaves the others: \( 1111_2 \mathbin{\&} 1101_2 = 1101_2 = 13 \). This is the clear-a-bit idiom, flag &= ~mask. The distractor d is the same value in hexadecimal, which %d does not print - it prints the decimal 13.

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

Post your comment

Please log in or register to post a comment.