/**
* 統計一個(int)數二進制數1的個數
* @param tag
* @return
*/
public static int countIntToBinaryOfOne(int tag){
int count=0;
while (tag!=0) {
if((tag&1)==1){//是1
count++;
}
tag>>>=1;//無符號右移一位
}
return count;
}
/**
* 最快速
* @param tag
* @return
*/
public static int countIntToBinaryOfOne1(int tag){
int count=0;
while (tag!=0) {
tag=(tag&(tag-1));
count++;
}
return count;
}
java 中如何快速統計一個int數轉二進制數中1的個數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.