Skip to the code
cf.
+25 XP

#670 Mirror a 32-Bit Mask

medium

A sprite editor stores a row of 32 pixels as a 32-bit mask, where bit 0 is the leftmost pixel. To flip the sprite horizontally, the bits must be reversed: bit 0 swaps with bit 31, bit 1 with bit 30, and so on.

mask is given as an unsigned value from 0 to 4294967295. Write mirrorMask(mask) that returns the reversed mask, also as an unsigned value.

Example 1

Input: mask = 1
Output: 2147483648

Only bit 0 is on. After mirroring, only bit 31 is on, which is 2^31 = 2147483648.

Example 2

Input: mask = 0
Output: 0

An empty row stays empty.

Constraints

  • 0 <= mask <= 4294967295
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).