Python bit manipulation


  • 0
    K
    class Solution(object):
        def findComplement(self, num):
            """
            :type num: int
            :rtype: int
            """
            leftmostSignificantPosition = 0
            n = num
            while n:
                n >>= 1
                leftmostSignificantPosition += 1
    
            return num ^ ((1 << leftmostSignificantPosition) - 1)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.