ProgrammingHardTCS NQT · Infosys pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int x = 12;
printf("%d %d %d", x & 1, x | 1, x ^ 1);- A. 1 13 13
- B. 0 13 13
- C. 0 12 12
- D. 1 12 13
Correct answer: B. 0 13 13
12 is 0b1100. Bit 0 of 12 is 0, so x & 1 = 0 (even check). x | 1 forces bit 0 on: 0b1101 = 13. x ^ 1 flips bit 0: since it was 0, it becomes 1 too: 0b1101 = 13. Output: 0 13 13.
More C Programming questions
- Assume 32-bit int. What is printed? c int a = 6, b = 3; printf("%d %d %d %d", a & b, a | b, a ^ b, ~a);
- Assume 32-bit int. What is the value of n that checks whether the k-th bit (0-indexed from the LSB) of x is set, for x=44 and k=3, and…
- Assume 32-bit int. What does this snippet do, and what is printed for x=10? c int x = 10; int y = x & (x - 1); printf("%d", y);
- Assume 32-bit int. What is printed? c #define TOGGLE(flags, bit) ((flags) ^= (1 << (bit))) int flags = 0b0110; TOGGLE(flags, 1)…
- Assume 32-bit int. What is printed? c int count = 0; unsigned int x = 0xF0F0F0F0; while (x) { count += x & 1; x >>= 1; } printf("%d"…
- Assume 32-bit int. What is printed? c int x = 5; int y = x & -x; printf("%d", y);