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