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.
Returns one plus the index of the least significant 1-bit of x, or
if x is zero, returns zero.
Returns the number of leading 0-bits in x, starting at the most
significant bit position. If x is 0, the result is undefined.
Returns the number of trailing 0-bits in x, starting at the least
significant bit position. If x is 0, the result is undefined.
Returns the number of 1-bits in 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
gcc的__builtin_函数(注意前面是两个下划线)的更多相关文章
- Python 私有变量中两个下划线 _ _item 与 一个下划线的区别 _item
python中没有常量的说法, 但是可以通过元组实现一个常量 在python的私有变量中, 存在两个下划线 _ _item 与一个下划线 _item 的区别 前面带两个下划线的私有变量: 只能在本类中 ...
- python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名
python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名,如果在类中定义的就是类的私有成员. >>> dir(__builtin ...
- 【转】gcc的__builtin_函数介绍
转自:http://blog.csdn.net/jasonchen_gbd/article/details/44948523 GCC提供了一系列的builtin函数,可以实现一些简单快捷的功能来方便程 ...
- [转]gcc的__builtin_函数介绍
链接地址:https://blog.csdn.net/jasonchen_gbd/article/details/44948523
- Python中类的变量,一个下划线与两个下划线的区别
形似 功能 __xx 这是私有变量, 只有内部可以访问,外部不可以访问.但是也不是一定不可以访问,只要以 _类名__xx样式就可以访问 .但最好不要这样做,养成良好编程习惯 _x 这是实例 ...
- gcc 内置函数
关于gcc内置函数和c隐式函数声明的认识以及一些推测 最近在看APUE,不愧是经典,看一点就收获一点.但是感觉有些东西还是没说清楚,需要自己动手验证一下,结果发现需要用gcc,就了解一下. 有时候 ...
- 关于gcc内置函数和c隐式函数声明的认识以及一些推测
最近在看APUE,不愧是经典,看一点就收获一点.但是感觉有些东西还是没说清楚,需要自己动手验证一下,结果发现需要用gcc,就了解一下. 有时候,你在代码里面引用了一个函数但是没有包含相关的头文件,这个 ...
- 让gcc支持成员函数模板的trick
让gcc支持成员函数模板的trick 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循“署名-非商业用途-保持一致”创作公用协议 gcc 4.7.3 不支持成员 ...
- DirectX:函数可以连接任意两个filter
函数可以连接任意两个filter HRESULT ConnectFilters( IBaseFilter *pSrc, IBaseFilter *pDest ) { IPin *pIn = 0; IP ...
随机推荐
- Visual Studio + Qt:GetVarsFromMakefile任务意外失败
问题: IntelliSense报告找不到头文件: 编译时报告GetVarsFromMakefile任务意外失败. 解决: 删除从Visual Studio装的Qt插件: 从Qt官网下载最新的插件:h ...
- 【Python】解决使用pyinstaller打包Tkinker程序报错问题
问题描述 使用pyinstaller打包使用Tkinter编写的控制台程序,出现报错 15793 INFO: Adding Microsoft.Windows.Common-Controls to d ...
- scala 特质的应用
一.为类提供可以堆叠的改变 package com.jason.qianfeng trait Loggertest { def logger(msg: String) } trait ConsoleL ...
- webx入门
- java修饰符的权限范围
java有四个修饰符,分别为public/protected/default/private,这四个修饰符的权限范围是不一样的. public修饰的成员,在同类.同包.子类(继承自本类).其他包中都可 ...
- C++函数形参与实参交换
c++中函数的实参传递到形参的值是单向的,改变形参并不会影响实参. #include <iostream> using namespace std; void swap(int a, in ...
- Django-11-Form组件
1. 概述 Django的Form组件一般功能有: 验证用户输入 生成html代码 返回错误信息 创建Form类 from django.shortcuts import render, redire ...
- 简单的爬虫程序以及使用PYQT进行界面设计(包含源码解析)
由于这个是毕业设计的内容,而且还是跨专业的.爬虫程序肯定是很简单的,就是调用Yahoo的API进行爬取图片.这篇博客主要讲的是基础的界面设计. 放上源码,然后分部解析一下重要的地方.注:flickra ...
- 《算法图解》全本PDF下载附百度云链接
作者使用Python和图画来解释算法,找了好久才找到PDF版本,末尾附百度云链接~ 作者[美]Aditya Bhargava 译者袁国忠 类别 出版 / 非虚构 出版社人民邮电出版社 / 2017-0 ...
- hystrix简介
hystrix,框架,提供了高可用相关的各种各样的功能,然后确保说在hystrix的保护下,整个系统可以长期处于高可用的状态,100%. 高可用系统架构: 资源隔离.限流.熔断.降级.运维监控 资源隔 ...