코테준비 - Number of 1 Bits

정상화·2023년 2월 26일

LeetCode

목록 보기
167/222

Number of 1 Bits

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int cnt = 0;
        for (int i = 0; i < 32; i++) {
            if (n & 1) {
                cnt++;
            }
            n >>= 1;
        }
        return cnt;
    }
};
profile
백엔드 희망

0개의 댓글