输入一个整数 n ,输出该数32位二进制表示中1的个数。其中负数用补码表示。 
   数据范围: 
  
   即范围为: 
  
 10
2
十进制中10的32位二进制表示为0000 0000 0000 0000 0000 0000 0000 1010,其中有两个1。
-1
32
负数使用补码表示 ,-1的32位二进制表示为1111 1111 1111 1111 1111 1111 1111 1111,其中32个1
/**
 * 思路:将n与n-1想与会把n的最右边的1去掉,比如
 * 1100&1011 = 1000
 * 再让count++即可计算出有多少个1
 * @author skyace
 *
 */
public class CountOne {
	 public static int NumberOf1(int n) {
			int count = 0;
	        while(n!=0){
	            count++;
	            n = n&(n-1);
	        }
	        
	        return count;
	    }
}
***********方法0*************
    # 由于负数的存储为补码,但是二进制函数bin()对负数的表达则是用负数的原码,所以负数会出错
    # 所以对于负数,先用~得到各位相反的正数,然后用bin函数表示正数(由于正数bin的表示结果会是正常的),最后由于各位相反,所以正数中的1会是负数中的0,所以减去。
    # 总数是32位这个信息来源于错误案例,应该也可以通过c语言中int为32位推出吧
    def NumberOf1(self, n):
        # write code here
        if n >= 0:
            return bin(n).count('1')
        else:
            return 32 - bin(~n).count('1')
***********方法1*************
    def NumberOf1(self, n):
        # write code here
        count = 0
        for _ in range(32):
            count += n & 1
            n = n >> 1
        return count
                                                                                    public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        for(int i = 0; i < 32;i++){
            if(((n >> i) & 1) == 1){
                ++count;
            }
        }
        return count;
    }
}
public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while(n != 0){
            count++;
            n = n & ( n - 1);
        }
        return count;
    }
}
 class Solution:
    def NumberOf1(self, n):
        # write code here
        if n<0:
            n=n&0xffffffff         count=0
        for i in range(32):
#             解法1
#             if n&(1<<i) == 1<<i:
#                 count += 1
#             解法2
            if n != 0 :
                n = (n-1)&n
                count += 1
        return count public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        //正数时如果二进制最小位有1,则必定为奇数,此时计1然后右移一位即可
        if(n >= 0){
            while(n>0){
                if((n%2)==1){
                    n>>=1;
                    count++;
                }else{
                    n>>=1;
                }
            }
        }else{//负数时先使用无符号右移,将其变为正数,但是要记得此时如果最低位是1则要先加1
            if(n%2 == -1){
                count++;
            }
            n>>>=1;
            //变成正数后照抄正数代码即可
            while(n>0){
                if((n%2)==1){
                    n>>=1;
                    count++;
                }else{
                    n>>=1;
                }
        }
    }
        return count;
    }
}
//采用位运算符,且相当于直接寻找1的个数,因此无论从时间还是空间复杂度都应该相对较小 bitset题干已经表明输出32位二进制中的1的个数,则可以创建一个长度32的bitset,使用count()方法直接计算1的个数:
class Solution {
public:
     int  NumberOf1(int n) {
         bitset<32> bits(n);
         return bits.count();
     }
}; 
                                                                                    # -*- coding:utf-8 -*-
"""
评论区一翻,发现都是大佬。
全网python解法中,我的估计是最low的了( ̄ェ ̄;)
@author cxcxrs
"""
class Solution:
    def NumberOf1(self, n):
        # write code here
        if n == 0:
            return 0
        elif n > 0:  # 正数转化为二进制(不用bin方法)
            res = ""
            while n > 0:
                if n % 2 == 1:
                    res += "1"
                else:
                    res += "0"
                n = n // 2
            return res.count("1")
        else:
            zhen = list(bin(n)[3:])  # 负数转化为二进制(用了bin方法)
            zhen = ['0' for i in range(32 - len(zhen))] + zhen  # 二进制数前面补零,凑满32位
            for i in range(len(zhen)):  # 取反
                if zhen[i] == '1':
                    zhen[i] = '0'
                else:
                    zhen[i] = '1'
            for i in range(len(zhen) - 1, -1, -1):  # 加一
                if zhen[i] == '0':
                    zhen[i] = '1'
                    break
                else:
                    zhen[i] = '0'
            return zhen.count('1') class Solution {
public:
     int  NumberOf1(int n) {
         int count = 0;
         int temp = n;
         if(n==0) return 0;
         if(n==-2147483648) return 1;
         if(n<0) n=-n;
         while(n>0){
             if(n%2)
                 ++count;
             n >>= 1;
         }
         if(temp<0&&((-temp)%2))
             count = 33 - count;
         else if(temp<0&&(!((-temp)%2)))
             count = 32 - count;         
         return count;
     }
}; 
public class Solution { //从n的2进制形式的最右边开始判断是不是1 /* * 该解法如果输入时负数会陷入死循环, * 因为负数右移时,在最高位补得是1 * 二本题最终目的是求1的个数,那么会有无数个 * 1了。 */ //-------------可能陷入死循环的解法--------------------- public static int NumberOf1_CanNotUse(int n) { int count = 0; while (n != 0) { /* * 用1和n进行位与运算, * 结果要是为1则n的2进制形式 * 最右边那位肯定是1,否则为0 */ if ((n & 1) == 1) { count++; } //把n的2进制形式往右推一位 n = n >> 1; } return count; } //---------------正解-------------------------------- //思想:用1(1自身左移运算,其实后来就不是1了)和n的每位进行位与,来判断1的个数 private static int NumberOf1_low(int n) { int count = 0; int flag = 1; while (flag != 0) { if ((n & flag) != 0) { count++; } flag = flag << 1; } return count; } //--------------------最优解---------------------------- public static int NumberOf1(int n) { int count = 0; while (n != 0) { ++count; n = (n - 1) & n; } return count; } public static void main(String[] args) { //使用n=10,二进制形式为1010,则1的个数为2; int n = -10; System.out.println(n + "的二进制中1的个数:" + NumberOf1(n)); } }