gcc的__builtin_函数(注意前面是两个下划线)

说明:

GCC provides a large number of built-in functions other than the ones mentioned above. Some of these are for internal use in the processing of exceptions or variable-length argument lists and will not be documented here because they may change from time to time; we do not recommend general use of these functions.

GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with __builtin_ will always be treated as having the same meaning as the C library function even if you specify the-fno-builtinoption. (see C Dialect Options) Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function will be emitted.

 

— Built-in Function: int __builtin_ffs ( unsigned int x)

Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.

— Built-in Function: int __builtin_clz ( unsigned int x)

Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.

— Built-in Function: int __builtin_ctz ( unsigned int x)

Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.

— Built-in Function: int __builtin_popcount ( unsigned int x)

Returns the number of 1-bits in x.

— Built-in Function: int __builtin_parity ( unsigned int x)

Returns the parity of x, i.e. the number of 1-bits in x modulo 2.

(以上内容来源见参考哦)

1.__builtin_parity(unsigned int x)

统计一个数二进制表示中1的个数是偶数还是奇数

2.__builtin_popcount(unsigned int x)

统计一个数的二进制表示中1的个数

3.__builtin_ffs(unsigned int x)

找出一个数的二进制表示中从末尾开始到遇见第一个1的的位置

4.__builtin_clz(unsigned int x)

返回一个数二进制表示中前导零的数目

5.__builtin_ctz(unsigned int x)

返回一个数二进制表示中尾零的数目

 

试验代码如下:

 1 #include <iostream>
 2 #include <map>
 3 
 4 #define max_n 200005
 5 using namespace std;
 6 map<int,int> mp;
 7 long long a[max_n];
 8 int n;
 9 int main()
10 {
11     cout << __builtin_popcount(3) << endl; //3:11 output:2
12     cout << __builtin_popcount(7) << endl; //7:111 output:3
13 
14     cout << __builtin_parity(3) << endl; //output:0
15     cout << __builtin_parity(7) << endl; //output:1
16 
17     cout << __builtin_ffs(3) << endl; //output:1
18     cout << __builtin_ffs(10) << endl;//10:1010 output:2
19 
20     cout << __builtin_ctz(3) << endl; //output:0
21     cout << __builtin_ctz(10) << endl;//output:1
22 
23     cout << __builtin_clz(3) << endl;//output:30
24     cout << __builtin_clz(10) << endl;//output:28
25     return 0;
26 }

更多内置函数参见:http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Other-Builtins.html

 

转载于:https://www.cnblogs.com/zhanhonhao/p/11221354.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值