ProgrammingVery hardCompetitive coding (CP)

What is printed? Assume uint32t is 32 bits.

What is printed? Assume uint32_t is 32 bits.
#include <stdint.h>
#include <stdio.h>
uint32_t rotl(uint32_t x, unsigned k) {
    return (x << k) | (x >> ((32 - k) & 31));
}
int main(void) {
    printf("%X", (unsigned)rotl(0x80000001u, 4));
}
  1. A. 18
  2. B. 10
  3. C. 80000018
  4. D. 8

Correct answer: A. 18

Rotating 0x80000001 left by 4 moves the top nibble 8 down to the bottom and leaves the low bits 0x10, so the result is 0x18. The mask (32 - k) & 31 avoids a shift by 32, which would be undefined.

More C Programming questions

Exams that ask this topic