Discussion
What will be the output of the program?
#include <stdio.h>
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}
Answer: B
Correct answer: 12480
Trace the loop. c is 48, which is \( 110000_2 \), the character '0'. Each pass ORs it with the mask and prints the result as a character, then shifts the mask left.
Masks 1, 2, 4 and 8 set bits that 48 does not have, giving 49, 50, 52 and 56 - the characters 1, 2, 4 and 8.
The fifth mask is 16, and 48 is \( 32 + 16 \), so bit 4 is already set: OR changes nothing and 48 prints as 0. The output is 12480, and the final zero is the whole point - OR is idempotent.
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