Iam_Sandeep

Reverse bits of a given 32 bits unsigned integer. reverse bits

Jun 13th, 2022 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. class Solution:
  2.     def reverseBits(self, n: int) -> int:
  3.         mask=1
  4.         res=0
  5.         for i in range(32):
  6.             res=res<<1
  7.             if mask & n:
  8.                 res=res | 1
  9.             mask=mask<<1
  10.         return res
  11. ```
  12. Here we are reversing the number for 32 bits. Every time we are updating the mask annd res when necessary.
  13. Consider the condtion if mask & n which tells that whethere there is a 1 bit at pos log 2 (mask).
  14. If so we add 1 to res else we do nothing
  15. ```
  16.    
Add Comment
Please, Sign In to add comment