Number of 1 Bits

Easy
409.

Number of 1 Bits

Number of 1 Bits is an easy coding problem on bit manipulation and divide and conquer, asked in Amazon, Google, Microsoft and Apple interviews.

Write a function that takes a non-negative integer n and returns the number of 1 bits in its binary representation — its Hamming weight.

Example 1

Input
n = 11
Output
3
Explanation
11 is 1011 in binary.

Example 2

Input
n = 128
Output
1

Example 3

Input
n = 0
Output
0

Constraints

  • 0 <= n <= 2147483647

Follow-up: If this function is called many times, how would you optimise it?

Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} n
 * @return {number}
 */
var hammingWeight = function(n) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Number of 1 Bits — Solution in Python, Java & C++

Number of 1 Bits solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Number of 1 Bits is an easy coding problem on bit manipulation and divide and conquer, asked in Amazon, Google, Microsoft and Apple interviews.

Problem statement

Write a function that takes a non-negative integer n and returns the number of 1 bits in its binary representation — its Hamming weight.

Example 1

Input: n = 11
Output: 3
Explanation: 11 is 1011 in binary.

Example 2

Input: n = 128
Output: 1

Example 3

Input: n = 0
Output: 0

Constraints

  • 0 <= n <= 2147483647

Follow-up: If this function is called many times, how would you optimise it?

Hints for Number of 1 Bits

  1. Hint 1

    Test the lowest bit with n & 1, then shift right, until nothing is left.

  2. Hint 2

    n & (n - 1) clears the lowest set bit, so a loop over that runs once per 1 bit instead of once per bit.

  3. Hint 3

    Use a logical (unsigned) shift so the loop terminates.

How to solve Number of 1 Bits

n & (n - 1) clears the lowest set bit; loop until zero and count the iterations.

Approach

  1. While n != 0: n &= n - 1, increment the count.
  2. Return the count.

Why it works

Subtracting one borrows through the trailing zeros and flips the lowest set bit, so the AND removes exactly that bit each iteration.

Complexity

  • Time — O(popcount)
  • Space — O(1)

Pitfalls

  • In a signed 32-bit language use a logical shift if you shift instead, or the loop never ends for negatives.

Reference solution

Python

def hammingWeight(n: int) -> int:
    count = 0
    while n:
        n &= n - 1
        count += 1
    return count

Java

public static int hammingWeight(int n) {
    int count = 0;
    int v = n;
    while (v != 0) {
        v &= v - 1;
        count++;
    }
    return count;
}

C++

int hammingWeight(int n) {
    unsigned int v = (unsigned int) n;
    int count = 0;
    while (v) {
        v &= v - 1;
        count++;
    }
    return count;
}

JavaScript

var hammingWeight = function(n) {
    let count = 0;
    let v = n >>> 0;
    while (v > 0) {
        v &= v - 1;
        count++;
    }
    return count;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 146 bit manipulation problems · the whole catalogue

Learn the technique: Bit Manipulation