c++高效位运算函数之 __builtin_(GCC自带的一些builtin内建函数)

前因

想必大家学习的时候都见过__builtin_*格式的函数,这些是GCC编译器自带的内建函数。一般是基于不同硬件平台采用专门的硬件指令实现的,因此性能较高。

以下介绍一下几个特别有用的函数,关于GCC内建函数的更完整的内容可以参见 官方文档。

常用的内建函数

1. int __builtin_ffs (unsigned int x)

返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4。

int n = 1;//1
int m = 8;//1000
cout<<__builtin_ffs(n)<<endl;//输出1
cout<<__builtin_ffs(m)<<endl;//输出4

一共有三个函数,分别适用于不同的输入类型(unsigned int,unsigned long以及unsigned long long)。


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.
 
int __builtin_ffsl (unsigned long)
    Similar to __builtin_ffs, except the argument type is unsigned long.
 
int __builtin_ffsll (unsigned long long)
	Similar to __builtin_ffs, except the argument type is unsigned long long.
2. int __builtin_clz (unsigned int x)

返回前导的0的个数。
这个函数作用是返回输入数二进制表示从最高位开始(左起)的连续的0的个数;如果传入0则行为未定义。

一共有三个函数,分别适用于不同的输入类型(unsigned int,unsigned long以及unsigned long long)。

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.
 
int __builtin_clzl (unsigned long)
    Similar to __builtin_clz, except the argument type is unsigned long.
 
int __builtin_clzll (unsigned long long)
    Similar to __builtin_clz, except the argument type is unsigned long long.
  cout << __builtin_clz(1) << '\n'; // 31
  cout << __builtin_clz(3) << '\n'; // 30
  cout << __builtin_clz(5) << '\n'; // 29
  int n = 6;
  cout << 32 - __builtin_clz(n) << '\n'; //n 的二进制长度
3. int __builtin_ctz (unsigned int x)

返回后面的0个个数,和__builtin_clz相对。
这个函数作用是返回输入数二进制表示从最低位开始(右起)的连续的0的个数;如果传入0则行为未定义。

int n = 1;//1
int m = 8;//1000
cout<<__builtin_ctzll(n)<<endl;//输出0
cout<<__builtin_ctz(m)<<endl;//输出3

一共有三个函数,分别适用于不同的输入类型(unsigned int,unsigned long以及unsigned long long)。

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.
 
int __builtin_ctzl (unsigned long)
	Similar to __builtin_ctz, except the argument type is unsigned long.
 
int __builtin_ctzll (unsigned long long)
	Similar to __builtin_ctz, except the argument type is unsigned long long.
4. int __builtin_popcount (unsigned int x)

返回二进制表示中1的个数,如果传入0则返回 0 。

int n = 15; //二进制为1111
cout<<__builtin_popcount(n)<<endl;//输出4

一共有三个函数,分别适用于不同的输入类型(unsigned int,unsigned long以及unsigned long long)。

int __builtin_popcount (unsigned int x)
    Returns the number of 1-bits in x.
 
int __builtin_popcountl (unsigned long)
    Similar to __builtin_popcount, except the argument type is unsigned long.
 
int __builtin_popcountll (unsigned long long)
    Similar to __builtin_popcount, except the argument type is unsigned long long.
5.int __builtin_parity (unsigned int x)

返回x的奇偶校验位,也就是x的1的个数模2的结果。

int n = 15;//二进制为1111
int m = 7;//111
cout<<__builtin_parity(n)<<endl;//偶数个,输出0
cout<<__builtin_parity(m)<<endl;//奇数个,输出1

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

由于这些函数是内建函数,经过了编译器的高度优化,运行速度十分快(有些甚至只需要一条指令)。

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值