1. _builtin_popcount(x):该函数用于对整数中“1”的个数进行计数
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 5;
printf("Count of 1s in binary of %d is %d ",
n, __builtin_popcount(n));
return 0;
}
output:
Count of 1s in binary of 5 is 2.
2. __builtin_clz(x):该函数用于对整数中前导“0”的个数进行计数
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 16;
printf("Count of leading zeros before 1 in %d is %d",
n, __builtin_clz(n));
return 0;
}
/*
* output:
* Count of leading zeros before 1 in 16 is 27
*/
3. __builtin_ctz(x):该函数用于对整数中尾随的“0”的个数进行计数
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 16;
printf("Count of zeros from last to first occurrence of one is %d",__builtin_ctz(n));
return 0;
}
/*
* output:
* Count of zeros from last to first occurrence of one is 4
*/