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
๐ค Output