Discussion
Predict the output of following program.
#include <stdio.h>
int main()
{
char flag=0x0f;
flag &= ~0x02;
printf("%d",flag);
return 0;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development