Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Number of 1 Bits problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Number of 1 Bits problem solution, we need to write a function that takes an unsigned integer and returns the number of ‘1’ bits it has.

Leetcode Number of 1 Bits problem solution

Leetcode Number of 1 Bits problem solution in Python.

class Solution:
def hammingWeight(self, n):
    return bin(n).count('1')

Number of 1 Bits problem solution in Java.

public class Solution {

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

Problem solution in C++.

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int ans=0;
        while (n)
            ans += n&1, n=n>>1;
        return ans;
    }
};

Problem solution in C.

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

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes