Javatpoint Logo
Javatpoint Logo

Builtin functions of GCC compiler

What is GCC compiler?

GCC stands for GNU Compiler Collection, which is the collection of compilers which is generally used in C or C++ programs to convert the code into assembly language.

We have a lot of inbuilt functions provided by the GCC, which are as follows:

1. __builtin_popcount()

This function is used to return the number of set bits or number of 1s in a decimal number. It takes one argument as a decimal number and returns the number of 1s presented in it.

As we know, we can represent any decimal number in binary format, representing a series of 1s and 0s.

For example: int a = 12;

We can represent 12 as 1100 in the binary format, so the number of 1's in the given integer is 2.

C Example:

Output:

Builtin functions of GCC compiler

Note: We can use this function for long and long long data types, but the function name will be slightly changed.

For long datatype: __builtin_popcountl()

For long long data type: __builtin_popcountll()

C Example:

Output:

Builtin functions of GCC compiler

2. __builtin_parity()

This function is used to determine the parity of a number. Parity means the number of set bits is even or odd. So if the number of set bits is odd, then it will return true or return one. Else, it will return false or return 0.

For example:

int num=12;

In the above example, we can represent 12 in binary format as 1100, so it has 2 number of set bits, so for this example, the above function will give output as 0.

Example:

int num=13;

We can represent 13 in binary format as 1101, so it has 3 number of set bits, which is odd, and this function will return 1.

C Example:

Output:

Builtin functions of GCC compiler

Note: We can use this function for long and long long data types:
For long: __builtin_parityl()
For long long: __builtin_parityll()

C Example:

Output:

Builtin functions of GCC compiler

3. __builtin_clz()

This function is used to count the number of leading zeros before the leftmost set bit in the number.

As we know, we can represent the number in binary format or in the form of bits. An integer takes 4 bytes or 32 bits, so we can represent each integer in 32 bits and tell the leading zeros.

For example:

int num =12;

We can represent 12 as 00000000 00000000 00000000 00001100

So, the number of leading zero is 28.

C Example:

Output:

Builtin functions of GCC compiler

Similarly, we can count the number of leading zeros in long and long long data types.

C Example:

Output:

Builtin functions of GCC compiler

4. __builtin__ctz()

This function will return the trailing zeros in a number or number of zeros to the rightmost set bit.

For example:

int a= 12

Binary representation: 00000000 00000000 00000000 00001100

So the number of trailing zeros is 2.

C Example:

Output:

Builtin functions of GCC compiler

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:

Output:

Builtin functions of GCC compiler





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA