Number of 1 Bits

 https://leetcode.com/problems/number-of-1-bits/submissions/

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count=0;
        while(n>0){
            if((n&1)>0){
                count++;
            }
            n=n>>1;
        }
        return count;
    }
}; 

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count=0;
        while(n>0){
            count++;
            n=n&(n-1);
        }
        return count;
    }
};

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!