Builtin functions of GCC compiler
Certain built-in functions are present in the GCC compiler. These features are as follows:
Function_ built in_popcount (x)
The number of 1s in data of the integer type can be counted using this built-in function. Let's look at a builtin _popcount() function sample.
Example
if x = 4
100 is the binary value of 4
Output:
The number of ones in binary value 4 is 1.
Program
#include<iostream>
using namespace std;
int main() {
int n = 13; // 1101 is binary
cout << "Count of 1s in binary of "<< n <<" is " << __builtin_popcount(n);
return 0;
}
Output

Function_builtin_parity (x)
The parity of an integer can be checked using this built-in function. If the number shows even parity, it results false, otherwise it results true. Let's look at a _builtin parity() function sample.
Example:
if x = 7
As binary number of 7 is 111 it contains odd number of 1’s.
Output:
1 is 7’s parity
Program:
#include<iostream>
using namespace std;
int main () {
int n = 13; //1101 is binary
cout << "Parity of "<< n <<" is " << __builtin_parity (n);
return 0;
}
Output:

Function_builtin_clz (x)
The leading zeros of an integer are counted using this built-in function. Let's look at a _builtin clz() function sample.
Example:
b = 16
00000000 00000000 00000000 00010000is the binary form of the number 16
Output:
The number obtained is 27
Program:
#include<iostream>
using namespace std;
int main () {
int n = 13; //1101 is binary
//0000 0000 0000 0000 0000 0000 0000 1101 (32bit integer)
cout << "Leading zero count of "<< n <<" is " << __builtin_clz(n);
return 0;
}
Output:

Function_builtin_ctz (x)
The trailing zeros of an integer are counted using this built-in function. Count Trailing Zeros is what the ctz stands for. Let's look at a _builtin ctz() function sample.
Example
b = 16
00000000 00000000 00000000 00010000 is the binary form of number 16
Output:
the result obtained is ctz which equals to 4
Program:
#include <stdio.h>
intmain ()
{
intn = 16;
printf ("Count of zeros from last to first "Occurrence of one is %d",
__builtin_ctz(n));
return0;
}
Output:

// Code to find the GCC compiler's built-in functions
#include <stdio.h>
#include <stdlib.h>
intmain ()
{
intnum = 4;
intclz = 0;
intctz = 0;
intpop = 0;
intparity = 0;
pop = __builtin_popcount(num);
printf ("Number of one's in %d is %d
", num, pop);
parity = __builtin_parity(num);
printf ("Parity of %d is %d
", num, parity);
clz = __builtin_clz(num);
printf("Number of leading zero's in %d is %d
", num, clz);
// It does not work for signed values
clz = __builtin_clz(-num);
printf ("Number of leading zero's in %d is %d
", -num, clz);
ctz = __builtin_ctz(num);
printf ("Number of trailing zero's in %d is %d
", num, ctz);
return0;
}
Output:

GCC also provides other built_in_functions
In addition to the above-mentioned built-in features, GCC offers a sizable number of others. We do not advise using these routines for general use; some of them are intended for internal usage in the processing of exceptions or variable-length argument lists and are not described here since they may change at any time.
The remaining functions are offered to aid with optimization.
Except for built-ins that expand to library calls or that have equivalents in the standard C library (described below), all built-in functions in GCC are always expanded inline, negating the need for corresponding entry points, and making it impossible to determine their address. A compile-time error occurs if you try to utilise them in an expression other than a function call.
Many of the functions in the common C library have built-in counterparts in GCC. There are two versions of these functions: one with names that begin with the __builtin_ prefix and one without. Even if you set the -fno-builtin option, both forms have the same type (including prototype), address (when their address is taken), and meaning as the C library functions. The library function is called when one of these functions cannot be optimised because many of them are only optimised in specific circumstances.