ProgrammingHardTCS NQT · Infosys

What is printed? The program computes the parity of the set bits of 0b1101.

What is printed? The program computes the parity of the set bits of 0b1101.
#include <stdio.h>
int parity(unsigned x) {
    return x == 0 ? 0 : (int)(x & 1u) ^ parity(x >> 1);
}
int main(void) {
    printf("%d", parity(0xD));
}
  1. A. 0
  2. B. 3
  3. C. 1
  4. D. 2

Correct answer: C. 1

0b1101 has three set bits, and the recursive XOR of the low bits gives 1 for an odd count.

More C Programming questions

Exams that ask this topic