Skip to main content

Reverse Bits

Reverse bits of a given 32-bit unsigned integer Example: n = 43261596 (binary: 00000010100101000001111010011100) => 964176192 (binary: 00111001011110000010100101000000)

Given a 32-bit unsigned integer n, return the unsigned integer whose binary digits are n's 32 bits in reverse order. All 32 bits count, including leading zeros, so 2 becomes 1073741824. For example, 43261596 becomes 964176192.

Examples

You write

reverse_bits(n: int)

  1. Example 1

    Input
    n = 43261596
    returns
    964176192
  2. Example 2

    Input
    n = 2147483644
    returns
    1073741822

The pattern behind it

Bit Manipulation

I reach for it when

  • The statement bans extra space or restricts operators.
  • Numbers act as flags, masks or XOR pairs.

Say this before you type. Say what each bit means in this problem before you shift or mask anything.

All 12 Bit Manipulation problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Reverse Bitseasy