
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Builtin functions of GCC compiler in C++
When you want to write a program in C++, your compiler (like GCC) converts your code into computer language. While doing this, GCC offers some special functions called built-in functions.
The built-in functions are predefined functions by the compiler itself, but not provided by any standard library.
The GCC compiler provides several built-in functions. Some of these functions are listed below:
The __builtin_popcount(x) Function
This builtin function is used to count the number of 1s in an integer type data. Let us see an example of _builtin_popcount() function.
Example
This program counts how many 1s are present in the binary form of the number n using the _builtin_popcount() function.
For instance n = 13, the binary number of 13 is 1101. So the total count the number of 1s = 3.
#include<iostream> using namespace std; int main() { int n = 13; //The binary is 1101 cout << "Count of 1s in binary of "<< n <<" is " << __builtin_popcount(n); return 0; }
Following is the output to the above program:
Count of 1s in binary of 13 is 3
The __builtin_parity(x) Function
This builtin function is used to check the parity of a number. If the number has odd parity, it will return true, otherwise it will return false. Let us see an example of _builtin_parity() function.
Example
The program checks if the number of 1s in the binary form of the number n is odd or even.
Assume n = 13, the binary number of 13 is 1101. It is having odd number of 1's. So, the parity of 13 is 1.
#include<iostream> using namespace std; int main() { int n = 13; //The binary is 1101 cout << "Parity of "<< n <<" is "<< __builtin_parity(n); return 0; }
Following is the output to the above program:
Parity of 13 is 1
The __builtin_clz(x) Function
This builtin function is used to count the leading zeros of an integer. The clz stands for Count Leading Zeros. Let us see an example of _builtin_clz() function.
Example
This program counts how many zeros appear before the first 1 in the 32-bit binary form of the number n.
Here n = 13, the binary number is 1101 and the 32bits integer is 0000 0000 0000 0000 0000 0000 0000 1101. the first 1 appears at the 4th position from the left, so there are 28 zeros before it. Thus, it returns 28.
#include<iostream> using namespace std; int main() { int n = 13; //The binary is 1101 //0000 0000 0000 0000 0000 0000 0000 1101 (32bit integer ) cout << "Leading zero count of "<< n <<" is " << __builtin_clz(n); return 0; }
Following is the output to the above program:
Leading zero count of 13 is 28
The __builtin_ctz(x) Function
This builtin function is used to count the trailing zeros of an integer. The ctz stands for Count Trailing Zeros. Let us see an example of _builtin_ctz() function.
Example
The program counts how many zeros are at the end (right side) of the 32-bit binary form of the number n.
For instance: n = 12, the binary number is 1100 and its 32bits integer is 0000 0000 0000 0000 0000 0000 0000 1100. Starting from the right side, the first '1' appears at the 3rd position where there are 2 zeros at the end of the binary number. So, the function counts these trailing zeros as 2.
#include<iostream> using namespace std; int main() { int n = 12; //The binary is 1100 //0000 0000 0000 0000 0000 0000 0000 1100 (32bit integer ) cout << "Trailing zero count of "<< n <<" is " << __builtin_ctz(n); return 0; }
Following is the output to the above program:
Trailing zero count of 12 is 2