Number Complement

class Solution(object):
    def findComplement(self, num):
        """
        :type num: int
        :rtype: int
        """
        res=0
        i=0
        while num>0:
            if num&1 == 0:
                res+=2**i
            i=i+1
            num=num>>1
            #print "num: ", num
            #print "res: ", res
        return res

if __name__ == '__main__':
    s=Solution()
    print s.findComplement(5)

posted @ 2017-03-29 23:21  clq.lib  阅读(147)  评论(0编辑  收藏  举报