Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0190 - Reverse Bits
    0190 - Reverse Bits
    0190 - Reverse Bits
    0190 - Reverse Bits

    0190 - Reverse Bits

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/reverse-bits/

    Solution 1 - Bit Manipulation (Iterative)

    Loop through all 32 bits. Extract the least significant bit of n, shift the result left by 1 and add the extracted bit, then shift n right by 1. After 32 iterations, the result holds the reversed bits.

    TimeComplexity:O(1)TimeComplexity: O(1)TimeComplexity:O(1)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Bit Manipulation (OR with Position Shifting)

    For each of the 32 bits, extract the bit at position i and place it at position 31 - i in the result using bitwise OR. This avoids shifting the result incrementally and instead directly places each bit in its reversed position.

    TimeComplexity:O(1)TimeComplexity: O(1)TimeComplexity:O(1)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0190.1 - Reverse Bits - Solution 1 - Bit Manipulation (Iterative) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseBits(self, n: int) -> int:
            """Reverse Bits Function"""
            result = 0
            for i in range(32):
                result = (result << 1) + (n & 1)
                n >>= 1
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().reverseBits(43261596))     # 964176192
        print(Solution().reverseBits(4294967293))   # 3221225471
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0190.2 - Reverse Bits - Solution 2 - Bit Manipulation (OR with Position Shifting) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseBits(self, n: int) -> int:
            """Reverse Bits Function"""
            result = 0
            for i in range(32):
                bit = (n >> i) & 1
                result |= bit << (31 - i)
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().reverseBits(43261596))     # 964176192
        print(Solution().reverseBits(4294967293))   # 3221225471
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()