算法目的:统计整数的二进制表达中有多少个1
算法实现原理:使用位运算,正负号处理问题。
实现:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package algorithm_database;
/**
* @author zhengchao
*/
public class CountOne {
public static void main(String[] args){
int x =-1;
System.out.println("1的个数"+count1(x));
}
public static int count1(int x){
int res = 0;
while(x!=0){
res+=x&1; //&操作,都是1才为1,其他都是0
x>>>=1; //无符号右移一位,不能用x>>=1,这是一个死循环,见下面三个位运算符的解释
}
return res;
}
}
/*
<< :左移运算符,num<<1,相当于num*2;
>>:右移运算符,num>>1,相当于num/2;
>>>:无符号右移,忽略符号位,空位以0补齐。
*/
注意事项:要使用无符号右移。
输出结果:32