We have numerous built-in functions available in GCC, including:
1. __builtin_popcount
This function is designed to calculate and return the count of set bits, which are essentially the number of 1s, in a decimal value. It accepts a single parameter, which should be a decimal number, and then outputs the total count of 1s contained within that number.
As it's commonly understood, decimal numbers can be converted into binary format, which consists of a sequence of 1s and 0s.
For example: int a = 12;
We can express the number 12 as 1100 in binary form, which means there are 2 occurrences of the digit 1 in this integer.
C Example:
#include <stdio.h>
int main() {
int num = 12;
printf("number of set bits in %d is %d",num,__builtin_popcount(num));
return 0;
}
Output:
number of set bits in [value] is [value]
Note: We can use this function for long and long long data types, but the function name will be slightly changed.
For long datatype: _builtinpopcountl
For long long data type: _builtinpopcountll
C Example:
#include <stdio.h>
int main() {
long num = 4562;
long long num2= 456564;
printf("number of set bits in %ld is %d \n",num,__builtin_popcountl(num));
printf("number of set bits in %lld is %d",num2,__builtin_popcountll(num2));
return 0;
}
Output:
number of set bits in [value] is [value]
2. __builtin_parity
This function serves the purpose of assessing the parity of a given number, which refers to whether the count of set bits in the number is even or odd. Therefore, if the number of set bits is odd, the function will output true or 1. Otherwise, it will yield false or 0.
For example:
int num=12;
In the previously mentioned scenario, the binary representation of the number 12 is 1100, indicating that it contains 2 set bits. Hence, applying the function to this case will result in an output of 0.
Example:
int num=13;
We can express the number 13 in binary as 1101, resulting in 3 set bits, which is an odd count, causing the function to output 1.
C Example:
#include <stdio.h>
int main() {
int num = 12;
int num2= 13;
if(__builtin_parity(num)==1){
printf("parity of %d is odd \n",num);
}else{
printf("parity of %d is even \n",num);
}
if(__builtin_parity(num2)==1){
printf("parity of %d is odd \n",num2);
}else{
printf("parity of %d is even \n",num2);
}
}
Output:
number of set bits in [value] is [value]
Note: We can use this function for long and long long data types: For long: __builtin_parityl For long long: __builtin_parityll
C Example:
#include <stdio.h>
int main() {
long num = 4562;
long long num2= 45665655563;
if(__builtin_parityl(num)==1){
printf("parity of %ld is odd \n",num);
}else{
printf("parity of %ld is even \n",num);
}
if(__builtin_parityll(num2)==1){
printf("parity of %lld is odd \n",num2);
}else{
printf("parity of %lld is even \n",num2);
}
}
Output:
number of set bits in [value] is [value]
3. __builtin_clz
This function is utilized to determine the quantity of leading zeros preceding the leftmost set bit within the given number.
As it is commonly understood, numbers can be represented in binary form or as a series of bits. An integer typically occupies 4 bytes or 32 bits, allowing us to express each integer using 32 bits and identify any leading zeros present.
For example:
int num =12;
We can express the number 12 as 00000000 00000000 00000000 00001100.
So, the number of leading zero is 28.
C Example:
#include <stdio.h>
int main() {
int num = 12;
printf("number of leading zeros in %d is %d",num,__builtin_clz(num));
}
Output:
number of set bits in [value] is [value]
Likewise, we have the capability to determine the quantity of leading zeros present in long and long long data types.
C Example:
#include <stdio.h>
int main() {
long num = 6545655;
long long num2= 5656566565565;
printf("number of leading zeros in %ld is %d\n",num,__builtin_clzl(num));
printf("number of leading zeros in %lld is %d",num2,__builtin_clzll(num2));
}
Output:
number of set bits in [value] is [value]
4. __builtin__ctz
This function will provide the count of trailing zeros in a number or the number of zeros up to the rightmost set bit.
For example:
int a= 12
Binary form: 00000000 00000000 00000000 00001100
So the number of trailing zeros is 2.
C Example:
#include<stdio.h>
int main(){
int num = 12;
printf("number of trailing zeros in %d is %d",num,__builtin_ctz(num));
}
Output:
number of set bits in [value] is [value]
Note: For long and long long data types, we can use this function as follows: For long data type: __builtin_ctzl For long long data type : __builtin_ctzll
C Example:
#include <stdio.h>
int main() {
long num = 654566;
long long num2= 565656656556;
printf("number of trailing zeros in %ld is %d\n",num,__builtin_ctzl(num));
printf("number of trailing zeros in %lld is %d",num2,__builtin_ctzll(num2));
}
Output: