DSA Tracker

Problem

Pattern this teaches:Bit Manipulation

Easy

Reverse Bits

An easy Bit Manipulation interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Bit Manipulation
Sheets
2
Core for
9 roles

The problem

Reverse the bits of a given 32-bit unsigned integer.

Example 1

Input
n = 43261596 (binary: 00000010100101000001111010011100)
Output
964176192 (binary: 00111001011110000010100101000000)
Why
The bits are reversed: the most significant bit becomes the least significant bit and vice versa.

Example 2

Input
n = 4294967293 (binary: 11111111111111111111111111111101)
Output
3221225471 (binary: 01111111111111111111111111111111)
Why
All bits except the least significant are 1. Reversing puts the 0 at the most significant position.

Constraints

  • The input must be a binary string of length 32

How to think about it

Updated 2026-09-29

Reversing a fixed-width binary register is mirror reflection, not trimming. Every trailing zero in the input becomes a leading zero in the output, and every leading zero becomes a trailing zero. You must always populate all thirty-two positions even when high-order bits contain nothing but zeroes.

Approaches, worst first

  1. Bit-by-bit extraction and push

    time O(1) · space O(1)

    Iterate thirty-two times. In each round, shift the result accumulator left by one, read the lowest bit of n using bitwise AND, append it to the accumulator, and logically right-shift n. Straightforward and dependable for fixed word sizes.

  2. Divide and conquer bit mask swapsWrite this one

    time O(1) · space O(1)

    Swap adjacent 16-bit halves, then adjacent 8-bit bytes, then 4-bit nibbles, then 2-bit pairs, and finally odd and even bits using constant bitmasks. Reduces the operation count to five parallel mask-and-shift steps.

Where people lose marks · 3
  • Terminating the loop early when `n === 0` discards the remaining unshifted positions, failing to push the required leading zeroes into high bit positions.
  • Using signed 32-bit bitwise left shifts in languages like JavaScript can flip the sign bit, turning large reversed values negative unless coerced back using unsigned right-shift `>>> 0`.
  • Using arithmetic shift `>>` instead of logical unsigned shift `>>>` fills incoming high bits with ones whenever the sign bit is active.

The theory behind it

Bit Manipulation — the ground this problem stands on. All Bit Manipulation problems

What Bit Manipulation is

Bit manipulation is the practice of working directly on the individual ones and zeros that form numbers in computer memory. Every integer is stored as a tiny row of electrical switches that are either on or off. Instead of running arithmetic loops, bitwise operations flip, mask, shift, or combine these switches in a single hardware cycle. This allows compact storage of sets and blazing-fast checks without allocating extra memory.

When to reach for it

Reach for bit manipulation when problems ask to find a unique non-duplicate number, count set bits, determine if a value is a power of two, or pack a set of small booleans into a single integer. Prompts mentioning constant O(1) auxiliary space constraints on array queries often hint at XOR cancellation. It is also the foundation of bitmask dynamic programming, where subsets of up to twenty items are tracked as integer masks.

How the pattern works

Think of an integer as a fixed-length set of flags. Use bitwise AND to inspect if a specific bit is set, bitwise OR to turn a bit on, and bitwise XOR to flip a bit or cancel out matched pairs. Learn the standard bit tricks: n AND with n minus one clears the lowest set bit, which counts ones quickly, while n AND with negative n isolates the lowest set bit. When packing sets into masks, represent the empty set as zero and add item i by shifting one left by i and combining with OR.

More Bit Manipulation problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .