Reverse Bits

easy

Reverse bits of a 32-bit unsigned integer

Reverse Bits

Key Insight

Extract LSB with (n & 1), shift result left, OR to add bit. Repeat 32 times.

Step 1Input
input
13
=
0
0
0
0
1
1
0
1
76543210
8-bit: 00001101

Reverse bits of 13 (binary: 1101)

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Reverse Bits

Extract LSB with (n & 1), shift result left, OR to add bit. Repeat 32 times.

  1. Input

    Reverse bits of 13 (binary: 1101)

  2. Extract LSB

    13 & 1 = 1. Add to result.

  3. Build Result

    Shift result left, extract next bit, OR it in.

  4. Continue

    Repeat for all 8 bits (or 32 for full int).

  5. Result

    00001101 reversed = 10110000 (176)