ProgrammingVery hardCompetitive coding (CP)

How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program.

How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program.
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint16_t m = (uint16_t)~(0xAAAAu | 0x5555u);
    int c = 0;
    for (int i = 0; i < 16; i++)
        if ((m >> i) & 1u) c++;
    printf("%d", c);
}
  1. A. 0
  2. B. 1
  3. C. 16
  4. D. 8

Correct answer: A. 0

0xAAAA and 0x5555 together set every bit of the 16-bit word, so their complement has no set bits and the count is 0.

More C Programming questions

Exams that ask this topic