GATE Computer ScienceHardGATE CS (PYQ pattern)

unsigned char rev(unsigned char x) { unsigned char r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } return r; } unsigned char rotl(unsigned char x, int k) { return (unsigned char)((x << k) | (x >> (8 - k))); } What is the decimal value of rotl(0x2D, 3) ^ rev(0x2D)?

  1. A. 220
  2. B. 68
  3. C. 17
  4. D. 221

Correct answer: D. 221

0x2D = 0010 1101. Rotating left by 3 moves the top three bits 001 to the bottom: 0110 1001 = 0x69. Reversing the bit order gives 1011 0100 = 0xB4. XOR: 0110 1001 ^ 1011 0100 = 1101 1101 = 221. A plain shift (losing the top bits) gives 220.

More Programming and Data Structures (GATE) questions

Exams that ask this topic