Discussion
What will be the output of the program?
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(P)
Answer: D
Correct answer: 0
The macro machinery is a distraction: M(P) expands to a main containing the printf, so the whole program reduces to printing -1 ^ ~0.
Now the arithmetic. ~0 inverts every bit of zero, giving all ones, which as a signed int is \( -1 \). So the expression is -1 ^ -1, and XOR of any value with itself is zero, since no bit position differs from itself. The answer is 0 regardless of the width of an int, which is what makes the identity \( x \mathbin{\hat{}} x = 0 \) worth knowing.
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