EASY NC#147 Blind #15 Bit Manipulation

190. Reverse Bits

๐Ÿ“– Problem

Reverse bits of a given 32 bits unsigned integer.

๐Ÿง  Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • โ€ขArray methods (`push`, `pop`, `shift`, `slice`)
  • โ€ขObject/Map/Set usage patterns
  • โ€ขFunction parameter and return typing

Logical Thinking Concepts

  • โ€ขDefine invariants before coding
  • โ€ขCheck edge cases first (`[]`, single element, duplicates)
  • โ€ขEstimate time/space before implementation
  • โ€ขApply Recursion reasoning pattern

๐Ÿ’ก Approach

  • โ†’ Extract each bit from the right and place it in the reversed position
  • โ†’ Use bit shifting and masking to manipulate individual bits
  • โ†’ Can be done iteratively or recursively
  • โ†’ Time: O(32) = O(1), Space: O(1)
  • โ†’ in functions, Byte swapping

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขExtract each bit from the right and place it in the reversed position
  • โ€ขUse bit shifting and masking to manipulate individual bits
  • โ€ขCan be done iteratively or recursively

Common Pitfalls

  • โ€ขTime: O(32) = O(1), Space: O(1)
  • โ€ขin functions, Byte swapping

๐Ÿงช Test Cases

Hidden tests on submit: 3

Test Case 1
Not run
Input:
reverseBits(0b00000010100101000001111010011100);
Expected:
964176192
Test Case 2
Not run
Input:
reverseBits(0b10000000000000000000000000000000);
Expected:
1
Test Case 3
Not run
Input:
reverseBits(0xFFFFFFFF);
Expected:
4294967295

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit