重拾编程之路--Number of 1 Bits

来源:互联网 发布:淘宝开店货源哪来 编辑:程序博客网 时间:2024/06/06 02:43

package com.lulu.leetcode;/** * 求无符号号数1的个数,存储为32位 * @author lulu * */public class C_hammingWeight {    public int hammingWeight(int n) {    int count=0;    for(int i=0;i<32;i++){    if((n>>>i & 1)==1){    count++;    }    }    return count;                    }/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}}


题意:求无符号整数以32位存储1的个数

 解题思路:

         无符号右移(>>>)i  [0,32)次,每次结果和1做与(&)运算,==1,计数加1;


0 0
原创粉丝点击