Reverse All Bits of an Even Signed Integer

Quick Overview

Reverse exactly 32 bits for the source’s even nonnegative signed-int domain, with safe internal shifts and repeated-call optimization.

Reverse All Bits of an Even Signed Integer

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Reverse the order of all 32 bits in the given signed-integer input and return the signed integer representing that reversed pattern. Leading zero bits participate; this is not reversal of only the significant digits. ```cpp int reverseBits(int n); ``` ### Examples ```text Input: n = 2 Output: 1073741824 ``` The bit at position 1 moves to position 30. ```text Input: n = 12 Output: 805306368 ``` ### Constraints - `0 <= n <= 2147483646`. - n is even. - Reverse exactly 32 bits; retain the signed int API. ### Optional follow-up How would you optimize repeated calls to this function?

Overview: Reverse exactly 32 bits for the source’s even nonnegative signed-int domain, with safe internal shifts and repeated-call optimization.

Bloomberg logo
Bloomberg
Oct 3, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Reverse the order of all 32 bits in the given signed-integer input and return the signed integer representing that reversed pattern. Leading zero bits participate; this is not reversal of only the significant digits.

int reverseBits(int n);

Examples

Input: n = 2
Output: 1073741824

The bit at position 1 moves to position 30.

Input: n = 12
Output: 805306368

Constraints

  • 0 <= n <= 2147483646 .
  • n is even.
  • Reverse exactly 32 bits; retain the signed int API.

Optional follow-up

How would you optimize repeated calls to this function?