Number of 1 Bits | Practice | GeeksforGeeks

Given a positive integer N, print count of set bits in it. 
Example 1:
Input:
N = 6
Output:
2
Explanation:
Binary representation is '110' 
So the count of the set bit is 2.
Example 2:
Input:
8
Output:
1
Explanation:
Binary representation is '1000' 
So the count of the set bit is 1.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function setBits() which takes an Integer N and returns the count of number of set bits.
Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 109
+1
ayush2024cs113412 hours ago
int setBits(int n) {                     int cnt;              while(n>0){          if((n&1)>0){              cnt++;}          n=n>>1;                                    }              return cnt;                        }
0
aravindmn75831 day ago
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

 // } Driver Code Ends
class Solution {
  public:
    int setBits(int N) {
        // Write Your Code here
       return  __builtin_popcount(N);
    }
};

// { Driver Code Starts.
int main() {
    int t;
    cin >> t;
    while (t--) {
        int N;
        cin >> N;

        Solution ob;
        int cnt = ob.setBits(N);
        cout << cnt << endl;
    }
    return 0;
}
  // } Driver Code Ends
+1
gaurabhkumarjha271020012 days ago
// one line solution in c++
  return  __builtin_popcount (N);
0
atulharsh274Premium1 week ago
//one line solution for this problem.
 return N?(N&1) + setBits(N>>1) : 0;
0
mankesh0161 week ago
    int cnt=1;
    
    while(N&=N-1)
        cnt++;
    
    return cnt;
+1
devadarsh72 weeks ago
Java fast solution| O(k) time-complexity, where k is the number of set bits(1's) |0.21s
class Solution {
    static int setBits(int n) {
        int x=0;
        while(n!=0)
        {   x=x+1;
            n=n&(n-1);
        }
        return x;
    }
}
0
rahuldwvdi3 weeks ago
Time Taken: 0.2/1.3
note: performing (n-1) makes the last set bit as 0 an all bits after it as 1 
class Solution {
    static int setBits(int n) {
        //brian kernighan approach
        int res = 0;
        while(n>0){
            n = n&(n-1);
            res++;
        }
        return res;
    }
}
0
milindprajapatmst191 month ago
class Solution {
  public:
    int setBits(int N) {
        int cnt = 0;
        while (N) {
            cnt += (N & 1);
            N >>= 1;
        }
        return cnt;
    }
};
0
jhadeepali26101 month ago
 int setBits(int N) {       int cnt=0;       while (N)       {           N=(N&(N-1));        //rightmost set bit is removed here           cnt++;       }       return cnt;   }
0
tirtha19025681 month ago
class Solution {
   static int setBits(int N) {
       int cnt = 0;
       while(N>0){
           int temp = N%2;
           if(temp == 1) cnt++;
           N = N/2;
       }
       return cnt;
   }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.