ExamAdda Logo

Number of 1 Bits

Easy

Given a 32-bit unsigned integer n, return the number of 1 bits in its binary representation.

This is also known as the Hamming weight of the integer.

For example, the binary representation of 11 is:

00000000000000000000000000001011

 

It contains three 1 bits, so the answer is 3.

Example 1

Input

n = 11

Output

3

Explanation

The 32-bit representation of 11 is:

00000000000000000000000000001011

 

There are three 1 bits.

Example 2

Input

n = 128

Output

1

Explanation

The 32-bit representation is:

00000000000000000000000010000000

 

There is only one 1 bit.

Constraints

0 <= n <= 2^32 - 1
n is a 32-bit unsigned integer.
Leading zeros in the 32-bit representation must be considered.

Hints:

Hint 1

Check the rightmost bit using:

n & 1

Hint 2

After checking a bit, shift the number right:

n >>= 1

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 23, 2026

Last updated

Aug 23, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output