ProgrammingVery hardProduct company OA

Assume 32-bit unsigned int. What is printed?

Assume 32-bit unsigned int. What is printed?
unsigned f(unsigned x) {
    return (x & 0x55555555u) << 1 | (x >> 1) & 0x55555555u;
}
printf("%X", f(0xB4u));
  1. A. 5A
  2. B. 50
  3. C. B4
  4. D. 78

Correct answer: D. 78

Precedence gives ((x & 0x55…) << 1) | ((x >> 1) & 0x55…): it swaps every pair of adjacent bits. 0xB4 = 10 11 01 00 becomes 01 11 10 00 = 0x78. '5A' is just x >> 1, and '50' is only the right-hand half of the OR.

More C Programming questions

Exams that ask this topic