ProgrammingHardGATE CS · Campus placements
uint32_t n = 0xB6u; the loop while (n) { n &= n - 1; count++; } runs with count starting at 0. What is count when the loop ends?
- A. 5
- B. 4
- C. 6
- D. 3
Correct answer: A. 5
n &= n - 1 clears the lowest set bit on each pass, so the loop runs once per set bit. 0xB6 = 1011 0110 has five 1 bits.
More C Programming questions
- uint16t v = 0xFFFFu; v = v << 4; What is v afterwards?
- For unsigned int n = 13, what is n ^ (n >> 1)? This is the binary-reflected Gray code of 13.
- 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?