Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Bit manipulation

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Bit manipulation

Reverse Bits

intermediatelayer I11 · Bit manipulation5 tests

Reverse the bits of an unsigned 32-bit integer n and return the resulting unsigned value. Bit 0 becomes bit 31, bit 1 becomes bit 30, and so on.

  • Always treat the input as exactly 32 bits wide, leading zeros included — reverseBits(1) is 2147483648, not 1
  • The JS trap: bitwise operators return a signed 32-bit result, so as soon as the reversal lands a 1 in bit 31 your accumulator goes negative. Finish with result >>> 0 to reinterpret it as unsigned.
  • Read individual bits with >>>, never >>

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen