Count Set-bits of number using Recursion - GeeksforGeeks

17 Nov, 2021
Given a number N. The task is to find the number of set bits in its binary representation using recursion.
Examples: 
Input : 21 Output : 3 21 represented as 10101 in binary representation
Input : 16 Output : 1 16 represented as 10000 in binary representation 
Approach:  
First, check the LSB of the number.If the LSB is 1, then we add 1 to our answer and divide the number by 2.If the LSB is 0, we add 0 to our answer and divide the number by 2.Then we recursively follow step (1) until the number is greater than 0.
First, check the LSB of the number.
If the LSB is 1, then we add 1 to our answer and divide the number by 2.
If the LSB is 0, we add 0 to our answer and divide the number by 2.
Then we recursively follow step (1) until the number is greater than 0.
Below is the implementation of the above approach :  
C++
Java
Python3
C#
Javascript
// CPP program to find number// of set bist in a number#include <bits/stdc++.h>using namespace std; // Recursive function to find// number of set bist in a numberint CountSetBits(int n){    // Base condition    if (n == 0)        return 0;             // If Least significant bit is set    if((n & 1) == 1)        return 1 + CountSetBits(n >> 1);         // If Least significant bit is not set    else        return CountSetBits(n >> 1);} // Driver codeint main(){    int n = 21;         // Function call    cout << CountSetBits(n) << endl;     return 0;}
// Java program to find number// of set bist in a numberclass GFG{    // Recursive function to find    // number of set bist in a number    static int CountSetBits(int n)    {        // Base condition        if (n == 0)            return 0;                     // If Least significant bit is set        if((n & 1) == 1)            return 1 + CountSetBits(n >> 1);                 // If Least significant bit is not set        else            return CountSetBits(n >> 1);    }         // Driver code    public static void main (String [] args)    {        int n = 21;                 // Function call        System.out.println(CountSetBits(n));    }} // This code is contributed by ihritik
# Python3 program to find number# of set bist in a number # Recursive function to find# number of set bist in a numberdef CountSetBits(n):         # Base condition    if (n == 0):        return 0;             # If Least significant bit is set    if((n & 1) == 1):        return 1 + CountSetBits(n >> 1);         # If Least significant bit is not set    else:        return CountSetBits(n >> 1); # Driver codeif __name__ == '__main__':    n = 21;         # Function call    print(CountSetBits(n)); # This code is contributed by 29AjayKumar
// C# program to find number// of set bist in a numberusing System; class GFG{    // Recursive function to find    // number of set bist in a number    static int CountSetBits(int n)    {        // Base condition        if (n == 0)            return 0;                     // If Least significant bit is set        if((n & 1) == 1)            return 1 + CountSetBits(n >> 1);                 // If Least significant bit is not set        else            return CountSetBits(n >> 1);    }         // Driver code    public static void Main ()    {        int n = 21;                 // Function call        Console.WriteLine(CountSetBits(n));    }} // This code is contributed by ihritik
<script> // Javascript program to find number// of set bist in a number // Recursive function to find// number of set bist in a numberfunction CountSetBits(n){         // Base condition    if (n == 0)        return 0;             // If Least significant bit is set    if ((n & 1) == 1)        return 1 + CountSetBits(n >> 1);         // If Least significant bit is not set    else        return CountSetBits(n >> 1);} // Driver codevar n = 21; // Function calldocument.write(CountSetBits(n)); // This code is contributed by Amit Katiyar </script>
3
 
Time Complexity: O(log n)
Auxiliary Space: O(log n)
ihritik
29AjayKumar
Akanksha_Rai
amit143katiyar
as5853535
kalrap615
simmytarika5
subhammahato348
setBitCount
Bit Magic
Recursion
Recursion
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Program to find parity
Hamming code Implementation in C/C++
Write an Efficient Method to Check if a Number is Multiple of 3
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Program for Sum of the digits of a given number