Count Set-bits of number using Recursion Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report Given a positive integer n, the task is to find the number of set bits in its binary representation using recursion.Examples: Input : 21 Output : 3 Explanation: 21 represented as 10101 in binary representation.Input : 16 Output : 1 Explanation: 16 represented as 10000 in binary representation.Using Recursion and Right Shift Operator - O(log n) time and O(log n) spaceThe idea is to recursively check the least significant bit in the number. If the bit is 1, increment the result. Right shift the number by 1, and recursively find the number of set bits in the computed number.Step by step approach: Recursively check the least significant bit of a number. Base case will return 0 if number is 0.If the least significant bit is 1, add 1 to result.Right shift the number by 1, to remove the least significant bit and recursively find the number of set bits in the computed number. C++ // C++ 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 number int setBits(int n) { // Base condition if (n == 0) return 0; // If Least significant bit is set if((n & 1) == 1) return 1 + setBits(n >> 1); // If Least significant bit is not set else return setBits(n >> 1); } int main() { int n = 21; cout << setBits(n) << endl; return 0; } Java // Java program to find number // of set bits in a number class GfG { // Recursive function to find // number of set bits in a number static int setBits(int n) { // Base condition if (n == 0) return 0; // If Least significant bit is set if((n & 1) == 1) return 1 + setBits(n >> 1); // If Least significant bit is not set else return setBits(n >> 1); } public static void main(String[] args) { int n = 21; System.out.println(setBits(n)); } } Python # Python program to find number # of set bits in a number # Recursive function to find # number of set bits in a number def setBits(n): # Base condition if n == 0: return 0 # If Least significant bit is set if (n & 1) == 1: return 1 + setBits(n >> 1) # If Least significant bit is not set else: return setBits(n >> 1) if __name__ == "__main__": n = 21 print(setBits(n)) C# // C# program to find number // of set bits in a number using System; class GfG { // Recursive function to find // number of set bits in a number static int setBits(int n) { // Base condition if (n == 0) return 0; // If Least significant bit is set if((n & 1) == 1) return 1 + setBits(n >> 1); // If Least significant bit is not set else return setBits(n >> 1); } static void Main() { int n = 21; Console.WriteLine(setBits(n)); } } JavaScript // JavaScript program to find number // of set bits in a number // Recursive function to find // number of set bits in a number function setBits(n) { // Base condition if (n === 0) return 0; // If Least significant bit is set if((n & 1) === 1) return 1 + setBits(n >> 1); // If Least significant bit is not set else return setBits(n >> 1); } let n = 21; console.log(setBits(n)); Output3 Related Article:Count set bits in an integer Create Quiz Comment T TheReciprocator Follow 2 Improve T TheReciprocator Follow 2 Improve Article Tags : Bit Magic Recursion DSA setBitCount Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like