codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

ProblemsNumber of 1 Bits

Number of 1 Bits

Easy
Bit ManipulationMath

Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight).

 

Example 1:

Input: n = 11
Output: 3
Explanation: The input binary string 1011 has a total of three set bits.

Example 2:

Input: n = 128
Output: 1
Explanation: The input binary string 10000000 has a total of one set bit.

 

Constraints:

  • 1 <= n <= 231 - 1

Related Problems

View all
Single Number
Easy•Bits
Counting Bits
Easy•Bits
Reverse Bits
Easy•Bits
Missing Number
Easy•Bits
Sum of Two Integers
Medium•Bits
Reverse Integer
Medium•Bits
Input:
n = 11