ProgrammingHardGATE CS · TCS NQT

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned x = 0xB5, y = 0x4E, z = x ^ y, c = 0;
    while (z) { z &= z - 1; c++; }
    printf("%u", c);
}
  1. A. 7
  2. B. 8
  3. C. 1
  4. D. 251

Correct answer: A. 7

x ^ y = 0xB5 ^ 0x4E = 0xFB = 1111 1011, and z &= z - 1 clears one set bit per pass, so c = 7 (the Hamming distance). 8 uses x | y = 0xFF, 1 counts the bits they share (x & y = 0x04), and 251 is the value of x ^ y, not its bit count.

More C Programming questions

Exams that ask this topic