clueless coding // TODO: be smarter

LeetCode Number of 1 Bits: CPP Solution



TL;DR Code Solution


int hammingWeight(uint32_t n) {

    int mask = 1;
    int count = 0;
    for (int i = 0; i < 32; i++) {
        if (mask & n) count++;
        mask = mask << 1;
    }
    return count;


}