ProgrammingHardGATE CS · Campus placements
For unsigned int n = 13, what is n ^ (n >> 1)? This is the binary-reflected Gray code of 13.
- A. 6
- B. 7
- C. 13
- D. 11
Correct answer: D. 11
Gray(n) = n ^ (n >> 1). With 13 = 1101 and 13 >> 1 = 0110, the XOR is 1011, which is 11. Taking only n >> 1 (6) is a common slip.
More C Programming questions
- What is the bitwise XOR of all integers from 1 to 10, that is 1 ^ 2 ^ 3 ^ ... ^ 10?
- What is the sum of popcount(i) for i = 1 to 8, where popcount counts the set bits?
- uint8t a = 0x3C, b = 0xA5; the statements a ^= b; b ^= a; a ^= b; run in that order. What are a and b afterwards?
- uint8t m = 0x0F; uint8t r = ~m; What is r?
- int a = -1; unsigned int b = 1u; What does if (a < b) evaluate to?
- int a = -7, b = 2; printf("%d %d", a / b, a % b); prints what?