My simple solution in Java


  • 7
    M

    the argument n is regarded as unsigned by us but signed by computer, so using "n % 2 == 1" to check if the lowest bit is 1 is wrong because it doesn't concern the highest bit indicate negative value. So we use "&" operation instead to check the lowest bit, and we use ">>>" (not ">>") to do logical shift right.

    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int counter = 0;
            while(n != 0) {
                if((n & 1) == 1) {
                    counter += 1;
                }
                n >>>= 1;
            }
            return counter;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.