首页 技术 正文
技术 2022年11月10日
0 收藏 374 点赞 2,687 浏览 2107 个字

说明:

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)

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

试验代码如下:

 #include <iostream>
#include <map> #define max_n 200005
using namespace std;
map<int,int> mp;
long long a[max_n];
int n;
int main()
{
cout << __builtin_popcount() << endl; //3:11 output:2
cout << __builtin_popcount() << endl; //7:111 output:3 cout << __builtin_parity() << endl; //output:0
cout << __builtin_parity() << endl; //output:1 cout << __builtin_ffs() << endl; //output:1
cout << __builtin_ffs() << endl;//10:1010 output:2 cout << __builtin_ctz() << endl; //output:0
cout << __builtin_ctz() << endl;//output:1 cout << __builtin_clz() << endl;//output:30
cout << __builtin_clz() << endl;//output:28
return ;
}

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285