# Builtin functions of GCC compiler

These are four important built-in functions in GCC compiler:

**_builtin_popcount(x):**This function is used to count the number of one’s(set bits) in an integer.

**Example:**if x = 4 binary value of 4 is 100 Output: No of ones is 1.

`// C program to illustrate _builtin_popcount(x)`

`#include <stdio.h>`

`int`

`main()`

`{`

`int`

`n = 5;`

`printf`

`(`

`"Count of 1s in binary of %d is %d "`

`,`

`n, __builtin_popcount(n));`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**Count of 1s in binary of 5 is 2

**_builtin_parity(x):**This function is used to check the parity of a number. This function returns true(1) if the number has odd parity else it returns false(0) for even parity.

**Example:**if x = 7 7 has odd no. of 1's in its binary(111). Output: Parity of 7 is 1

`// C program to illustrate _builtin_parity(x)`

`#include <stdio.h>`

`int`

`main()`

`{`

`int`

`n = 7;`

`printf`

`(`

`"Parity of %d is %d "`

`,`

`n, __builtin_parity(n));`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**

Parity of 7 is 1

**__builtin_clz(x):**This function is used to count the leading zeros of the integer. Note : clz = count leading zero’s

**Example:**It counts number of zeros before the first occurence of one(set bit).a = 16 Binary form of 16 is 00000000 00000000 00000000 00010000 Output: 27

`// C program to illustrate __builtin_clz(x)`

`#include <stdio.h>`

`int`

`main()`

`{`

`int`

`n = 16;`

`printf`

`(`

`"Count of leading zeros before 1 in %d is %d"`

`,`

`n, __builtin_clz(n));`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**Count of leading zeros before 1 in 16 is 27

**Note:**__builtin_clz(x) This function only accept unsigned values**__builtin_ctz(x):**This function is used to count the trailing zeros of the given integer. Note : ctz = count trailing zeros.

**Example:**Count no of zeros from last to first occurence of one(set bit).a = 16 Binay form of 16 is 00000000 00000000 00000000 00010000 Output: ctz = 4

`// C program to illustrate __builtin_ctz(x)`

`#include <stdio.h>`

`int`

`main()`

`{`

`int`

`n = 16;`

`printf`

`(`

`"Count of zeros from last to first "`

`"occurrence of one is %d"`

`,`

`__builtin_ctz(n));`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**Count of zeros from last to first occurrence of one is 4

- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- When does compiler create default and copy constructors in C++?
- Does C++ compiler create default constructor when we write our own?
- C++ Mathematical Functions
- Inline Functions in C++
- List in C++ | Set 2 (Some Useful Functions)
- Searching in a map using std::map functions in C++
- Macros vs Functions
- Pure Functions
- Functions that cannot be overloaded in C++
- isalpha() and isdigit() functions in C/C++ with example
- strdup() and strndup() functions in C/C++
- Const member functions in C++
- Ceil and Floor functions in C++
- How arrays are passed to functions in C/C++

`// C program to illustrate builtin functions of ` `// GCC compiler ` `#include <stdio.h> ` `#include <stdlib.h> ` ` ` `int` `main() ` `{ ` ` ` `int` `num = 4; ` ` ` `int` `clz = 0; ` ` ` `int` `ctz = 0; ` ` ` `int` `pop = 0; ` ` ` `int` `parity = 0; ` ` ` ` ` `pop = __builtin_popcount(num); ` ` ` `printf` `(` `"Number of one's in %d is %d\n"` `, num, pop); ` ` ` ` ` `parity = __builtin_parity(num); ` ` ` `printf` `(` `"Parity of %d is %d\n"` `, num, parity); ` ` ` ` ` `clz = __builtin_clz(num); ` ` ` `printf` `(` `"Number of leading zero's in %d is %d\n"` `, num, clz); ` ` ` ` ` `// It only works for unsigned values ` ` ` `clz = __builtin_clz(-num); ` ` ` `printf` `(` `"Number of leading zero's in %d is %d\n"` `, -num, clz); ` ` ` ` ` `ctz = __builtin_ctz(num); ` ` ` `printf` `(` `"Number of trailing zero's in %d is %d\n"` `, num, ctz); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Number of one's in 4 is 1 Parity of 4 is 1 Number of leading zero's in 4 is 29 Number of leading zero's in -4 is 0 Number of trailing zero's in 4 is 2

This article is contributed by **Maxx selva k**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.