Hyoseo Lee
Home About Projects Blog Thinking
leetcode

191. Number of 1 Bits

Topic Divide and Conquer
Area Algorithms
Summary
from moving on each bits, count the number of ones.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Divide and Conquer, Bit Manipulation

Intuition

this one seems easy and it was. Horray to BIT MANIPULATION!!!

Approach

from moving on each bits, count the number of ones.

Solution

int hammingWeight(int n) {
    int count = 0;
    while(n){
        if(n&1 == 1){
            count++;
        }
        n = n>>1;
    }
    return count;
}

Complexity

Thoughts

getting more and more familiar with BIT MANIPULATION!!!