介绍四种GCC内置位运算函数

张文泰 posted @ 2010年1月11日 01:27 in Code and Develop with tags 位运算 , 16289 阅读
  • int __builtin_ffs (unsigned int x)
    返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4。
  • int __builtin_clz (unsigned int x)
    返回前导的0的个数。
  • int __builtin_ctz (unsigned int x)
    返回后面的0个个数,和__builtin_clz相对。
  • int __builtin_popcount (unsigned int x)
    返回二进制表示中1的个数。
  • int __builtin_parity (unsigned int x)
    返回x的奇偶校验位,也就是x的1的个数模2的结果。

此外,这些函数都有相应的usigned long和usigned long long版本,只需要在函数名后面加上l或ll就可以了,比如int __builtin_clzll。


本作品遵循“署名-非商业性使用-相同方式共享 3.0 Unported”协议,转载请注明来自richard-desktop
Creative Commons License
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter