Prove safety offer- binary number 1 - binary conversion - complement inverted original code -python

Title Description

An integer that indicates the number of the output of the binary number 1 in. Wherein a negative number indicates a complement.
 
'' ' 
First determines whether n is not negative, when n is negative, directly after the while loop will lead to an endless loop, as a negative 
shifted to the left, then the highest bit up 1! A little special handling is required, may be the most significant bit of the symbol bit 1 to a 0, it 
is n & 0xffffffff, so put the positive to negative conversion, the only difference is the most significant bit changes from 1 to 0, because the less 
a 1, the count is incremented. After re-processing method according to positive numbers while loop operates can! '' ' 
# - * - Coding: UTF-. 8 - * - 
class Solution:
     DEF NumberOf1 (Self, n-):
         # Write code here Wallpaper 
        COUNT = 0
         IF n-< 0: 
            n- = n-& 0xFFFFFFFF
         the while n-: 
            COUNT + =. 1 
            n- = (n--. 1) & n-
         return COUNT

 

Guess you like

Origin www.cnblogs.com/ansang/p/12041887.html