ProgrammingModerateTCS NQT · Infosys
What is the sum of popcount(i) for i = 1 to 8, where popcount counts the set bits?
- A. 12
- B. 13
- C. 21
- D. 8
Correct answer: B. 13
The popcounts of 1..8 are 1,1,2,1,2,2,3,1, totalling 13; a quick check is that 1..7 hold 12 bits (each of 3 positions set 4 times) plus one for 8. 12 stops at 7, 21 counts binary digits rather than set bits, and 8 counts the zero bits.
More C Programming questions
- 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?
- #define SQUARE(x) xx. What does printf("%d", SQUARE(2+3)) print?
- #define MAX(a,b) ((a)>(b)?(a):(b)). With int i = 7; int m = MAX(i++, 5); what are m and i afterwards?