The Wayback Machine - https://web.archive.org/web/20240602214816/https://www.geeksforgeeks.org/cpp-__builtin_popcount-function/
Open In App

C++ __builtin_popcount() Function

Last Updated : 05 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

__builtin_popcount()  is a built-in function of GCC compiler. This function is used to count the number of set bits in an unsigned integer. In other words, it counts the number of 1’s in the binary form of a positive integer.

Syntax:

__builtin_popcount(int number);

Parameter: This function only takes unsigned or positive integers as a parameter.

Return Value: Number of set bits in the given number.

Illustration:

Input: n = 5
binary value of 5: 101
Output: 2

Example:

C++




// C++ code to demonstrate the
// __builtin_popcount function
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
 
    int n = 4;
 
    // Printing the number of set bits in n
    cout << __builtin_popcount(n);
 
    return 0;
}


Output

1

Time Complexity(b), where b is the number of bits.
Auxiliary Space: O(1)

What will happen if the data type is of type long long?

__builtin_popcount() is a GCC extension that is used to count the number of set bits in long long data types. 

Syntax:

__builtin_popcountll(long long number);

Example:

C++




// C++ code to demonstrate the
// __builtin_popcount function
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
 
    long long n = 1e15;
 
    // Printing the number of set bits in n
    cout << __builtin_popcountll(n);
 
    return 0;
}


Output

20


Similar Reads

error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th
9 min read
Difference between Virtual function and Pure virtual function in C++
Virtual Function in C++ A virtual function is a member function which is declared within a base class and is re-defined(Overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. Pu
2 min read
How to call some function before main() function in C++?
Since it is known that main() method is the entry point of the program. Hence it is the first method that will get executed by the compiler. But this article explains how to call some function before the main() method gets executed in C++. How to call some function before main() function? To call some function before main() method in C++, Create a
3 min read
How to call function within function in C or C++
When we begin programming in C/C++, we generally write one main() function and write all our logic inside this. This approach is fine for very small programs, but as the program size grows, this become unmanageable. So we use functions. We write code in the form of functions. The main function always acts as a driver function and calls other functi
3 min read
Difference between user defined function and library function in C/C++
Library Functions These functions are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc. To use these functions in the program the user has to use a header file associated with the
3 min read
Difference between virtual function and inline function in C++
Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments are evaluated only once. The syntax of defining th
2 min read
Difference Between Friend Function and Virtual Function in C++
A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This function can be invoked like a normal function and
3 min read
Returning a function pointer from a function in C/C++
In C/ C++, like normal data pointers(int *, char *, etc), there can be pointers to functions. Every function created in a program gets an address in memory since pointers can be used in C/C++, so a pointer to a function can also be created. Syntax: return type (*function_pointer_name) (argument_type_1, argument_type_2, ......, argument_type_n) =
6 min read
What happens when a virtual function is called inside a non-virtual function in C++
Predict the output of the following simple C++ program without any virtual function. C/C++ Code #include &amp;lt;iostream&amp;gt; using namespace std; class Base { public: void print() { cout &amp;lt;&amp;lt; &amp;quot;Base class print function \n&amp;quot;; } void invoke() { cout &amp;lt;&amp;lt; &amp;quot;Base class invoke function \n&amp;quot;;
2 min read
Function Overloading vs Function Overriding in C++
Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesn’t play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); C/C++ Code
3 min read
Practice Tags :