Discussion
Predict the output of following program.
#include <stdio.h>
int main()
{
int a=10;
int b=2;
int c;
c=(a & b);
printf("c= %d",c);
return 0;
}
Answer: C
Correct answer: c= 2
Write both operands in binary and AND them position by position:
\[ 10 = 1010_2 \qquad 2 = 0010_2 \]
A result bit is 1 only where both inputs are 1, which happens in the second position alone, giving \( 0010_2 = 2 \).
The distractor 12 is what | would give and 0 is what ^ would give in the other direction - useful checks, since 10 and 2 share exactly one bit. Note that && would have given 1, both operands being non-zero.
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