bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 190. Reverse Bits

190. Reverse Bits

Leetcode Solutions

Approach: Bit by Bit

  1. Initialize result to 0.
  2. Loop for 32 iterations since the integer is 32 bits long. a. Isolate the right-most bit of n using n & 1. b. Shift this bit to its reversed position, which is 31 - i where i is the current iteration index. c. Use the OR operation to add the reversed bit to the result. d. Shift n to the right by 1 to process the next bit.
  3. Return result.
UML Thumbnail

Approach: Mask and Shift

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...