__builtin_ 位运算

本文介绍了C++中与位运算相关的内置函数,包括`__builtin_ffs`用于查找最低位的1并返回其位置,`__builtin_clz`计算最高位前的0位数,`__builtin_ctz`统计最低位后的0位数,`__builtin_popcount`计算1的个数,以及`__builtin_parity`求奇偶校验位。这些函数在处理位操作时非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

ffs - find first bit set
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
x的最后一位1是从后向前第几位,位置从1计数,找不到返回0;

— 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.
统计左起第一个‘1’之前(头部)0的个数。

— 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.
统计右起第一个‘1’之后(尾部)的0的个数。

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

Returns the number of 1-bits in x.
返回‘1’的个数。

— 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.
返回x的奇偶校验位,也就是x中1的个数模2的结果。

另外,函数名后加 l,参数为unsigned long,加 ll,为unsigned long long

— Built-in Function: int __builtin_ffsl (unsigned long)
— Built-in Function: int __builtin_ffsll (unsigned long long)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值