Reverse the Bit Order of a 32-Bit Permission Mask

Last updated: November 29, 2026

Quick Overview

Given an unsigned 32-bit integer representing a permission mask, return the integer formed by reversing the order of its 32 bits.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given an unsigned 32-bit integer representing a permission mask, return the integer formed by reversing the order of its 32 bits.

A quick Microsoft phone screen warm-up that probes fluency with shifts and masks, the everyday tools behind Windows access masks and protocol headers. The interviewer often follows up by asking how to make it fast when called millions of times.

What the Interviewer Expects
  • Loop 32 times, shifting the result left and taking the low bit
  • Always process exactly 32 bits, including leading zeros
  • Keep the result unsigned
  • Offer the divide-and-conquer swap or lookup-table optimization
  • State O(1) time for fixed width
Key Topics to Cover
bit manipulation
shifts
masks
lookup tables
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Reverse with five mask-and-swap steps instead of a loop.
  • How would a 256-entry byte lookup table speed this up?
  • How does this differ from reversing byte order (endianness)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is an unsigned 32-bit value (in Python, an int from 0 to 2^32 - 1). Bit 0 must move to bit 31, bit 1 to bit 30, and so on. Leading zeros count: reversing 1 gives 2^31, not 1....

High-Level Design

The straightforward method peels bits off the low end of x and pushes them onto the low end of the result, shifting the result left each time. After exactly 32 iterations, the first bit read (x's bit ...


Submit Your Answer
Markdown supported