ProgrammingHardTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned a = 0x12345678u, b = 0x12345600u, x = a ^ b;
    int c = 0;
    while (x) { c += x & 1u; x >>= 1; }
    printf("%d", c);
}
  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 8

Correct answer: A. 4

a ^ b is 0x78, which has four set bits, so these two values differ in exactly four bit positions.

More C Programming questions

Exams that ask this topic