basics · intermediate · ~15 min

Rotate right

Circularly shift bits toward the LSB.

Challenge

Implement:

uint32_t rotr32(uint32_t x, int n);

Rotate x right by n positions. n>=0; rotation is mod 32.

Input format

A 32-bit value and a shift n.

Output format

The rotated value.

Constraints

n is reduced mod 32.

Starter code

#include <stdint.h>
/* Rotate x right by n positions (n>=0; rotation is mod 32). */
uint32_t rotr32(uint32_t x,int n){ (void)n; return x; }

Common mistakes

The 32-n shift is undefined when n==0 — guard it.

Edge cases to handle

rotr by n then by 32-n returns x.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.