How to Count Set Bits in an Integer in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In binary representation of a number, a set bit is defined as the binary digit (bit) that is set to 1. In this article, we will learn how to count the set bits in a given integer in C++.ExampleInput: 13Output:The number of set bits in 13 (1101) is: 3Counting Set Bits in an IntegerTo count the set bits in a number in C++, we can directly use the __builtin_popcount() method provided by the GCC compiler in C++. This function returns the number of set bits in a given unsigned integer.Syntax__builtin_popcount(int x);Here, x is the unsigned or positive number for which you want to determine the number of set bits.C++ Program to Count Number of Set Bits in an IntegerThe following program illustrates how we can count the set bits of a number using the __builtin_popcount() method in C++. C++ // C++ Program to Count Set Bits #include <bitset> #include <iostream> using namespace std; // Function to count set bits in an integer and print the // result void countAndPrintSetBits(int num) { // Calculate number of set bits using GCC's built-in // function for counting set bits int count = __builtin_popcount(num); // Print the number of set bits and binary // representation of the number cout << "Number of set bits in " << num << " (" << bitset<32>(num).to_string().substr( bitset<32>(num).to_string().find('1')) << ") is: " << count << endl; } int main() { // Declare the numbers int num1 = 5; int num2 = 13; // Count and print set bits for num1 countAndPrintSetBits(num1); // Count and print set bits for num2 countAndPrintSetBits(num2); return 0; } OutputNumber of set bits in 5 (101) is: 2 Number of set bits in 13 (1101) is: 3 Time Complexity: O(1)Auxiliary Space: O(1)To learn about other methods for counting set bits in an integer you may refer to this article : Counting Set Bits in an integer Comment More infoAdvertise with us Next Article Must Coding Questions - Company-wise N nikunj_sonigara Follow Improve Article Tags : C++ Programs C++ CPP-bitset C++ Bit Manipulation Practice Tags : CPP Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like