ProgrammingVery hardGATE CS · Campus placements
How many integers in the range 0 to 15 have an even number of 1 bits in binary?
- A. 11
- B. 8
- C. 7
- D. 2
Correct answer: B. 8
By popcount, 0..15 split as 1, 4, 6, 4, 1, so even popcounts give 1 + 6 + 1 = 8 (exactly half, as for any full range 0..2^k - 1). 7 forgets that 0 has zero (even) set bits, 2 forgets the six values with two bits, and 11 counts values with at least two bits.
More C Programming questions
- How many 1 bits are set in 2^20 - 1 (a 32-bit unsigned value)?
- uint8t a = 16; a = a a; What is a?
- uint32t t = 2000000000u + 2000000000u; What is t?
- char buf[10]; char a = buf + 2, b = buf + 9; What does printf("%td", b - a) print?
- struct P { int x, y; } arr[4]; struct P p = arr; p += 2; What is p - arr?
- #define MIN(a,b) ((a)<(b)?(a):(b)). What does printf("%d", MIN(MIN(3,1),2)) print?