ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int flags = 0;
flags |= (1 << 2);
flags |= (1 << 4);
flags &= ~(1 << 2);
printf("%d", flags);- A. 4
- B. 20
- C. 12
- D. 16
Correct answer: D. 16
flags starts 0, sets bit2 (0b00100=4), sets bit4 (0b10100=20), then clears bit2 via AND with the complement of (1<<2): 20 & ~4 = 0b10100 & 0b...11011 = 0b10000 = 16.
More C Programming questions
- What is printed? c unsigned int x = 10; printf("%d", (x << 1) == (x 2)); printf(" %d", (x >> 1) == (x / 2));
- Assume 32-bit int, two's complement. What is printed? c int x = ~0; printf("%d %u", x, (unsigned int)x);
- Assume 32-bit int. What is printed? c int x = 13; int count = 0; while (x) { count += x & 1; x >>= 1; } printf("%d", count);
- Assume 32-bit int. What is printed? c int x = -5; printf("%d", x & 1);
- Assume 32-bit int, two's complement. What is printed? c int x = 7; int y = -7; printf("%d %d", x | y, x ^ y);
- What is printed? c int a = 0; for (int i = 0; i < 4; i++) { a |= (1 << i); } printf("%d", a);