glibc的几个有用的处理二进制位的内置函数(转)
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.
— Built-in Function: int __builtin_ffsl (unsigned long)
Similar to __builtin_ffs, except the argument type is unsigned long.
— Built-in Function: int __builtin_clzl (unsigned long)
Similar to __builtin_clz, except the argument type is unsigned long.
— Built-in Function: int __builtin_ctzl (unsigned long)
Similar to __builtin_ctz, except the argument type is unsigned long.
— Built-in Function: int __builtin_popcountl (unsigned long)
Similar to __builtin_popcount, except the argument type is unsigned long.
— Built-in Function: int __builtin_parityl (unsigned long)
Similar to __builtin_parity, except the argument type is unsigned long.
— Built-in Function: int __builtin_ffsll (unsigned long long)
Similar to __builtin_ffs, except the argument type is unsigned long long.
— Built-in Function: int __builtin_clzll (unsigned long long)
Similar to __builtin_clz, except the argument type is unsigned long long.
— Built-in Function: int __builtin_ctzll (unsigned long long)
Similar to __builtin_ctz, except the argument type is unsigned long long.
— Built-in Function: int __builtin_popcountll (unsigned long long)
Similar to __builtin_popcount, except the argument type is unsigned long long.
— Built-in Function: int __builtin_parityll (unsigned long long)
Similar to __builtin_parity, except the argument type is unsigned long long.
#include <stdio.h>
#include <iostream>
#include <bitset>
#include <string>
#include <limits.h> using namespace std; uint32_t g_arr[] = {, , , , , , , , , , UINT_MAX-, UINT_MAX}; string g_str_func[] = {
"__builtin_ffs",
"__builtin_clz",
"__builtin_ctz",
"__builtin_popcount",
"__builtin_parity"
}; //typedef int (*fp_builtin_t)(unsigned int); //fp_builtin_t g_func[] = {
//__builtin_ffs,
//__builtin_clz,
//__builtin_ctz,
//__builtin_popcount,
//__builtin_parity
//}; int main()
{
/* for (int k = 0; k < 5; k ++) {
printf("%s:\n", g_str_func[k].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
fp_builtin_t fp_func = g_func[k];
printf("%u(%s): %d\n", t, b.to_string().c_str(), fp_func(t));
} puts("");
}
*/ // ffs
printf("%s:\n", g_str_func[].c_str());
for (int i = ; i < ; i ++) {
int t = g_arr[i];
bitset<> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ffs(t));
}
puts(""); // clz
printf("%s:\n", g_str_func[].c_str());
for (int i = ; i < ; i ++) {
int t = g_arr[i];
bitset<> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_clz(t));
}
puts(""); // ctz
printf("%s:\n", g_str_func[].c_str());
for (int i = ; i < ; i ++) {
int t = g_arr[i];
bitset<> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ctz(t));
}
puts(""); // popcount
printf("%s:\n", g_str_func[].c_str());
for (int i = ; i < ; i ++) {
int t = g_arr[i];
bitset<> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_popcount(t));
}
puts(""); // parity
printf("%s:\n", g_str_func[].c_str());
for (int i = ; i < ; i ++) {
int t = g_arr[i];
bitset<> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_parity(t));
}
puts("");
return ;
}
【测试结果】
__builtin_ffs:
():
():
():
():
():
():
():
():
():
():
():
(): __builtin_clz:
():
():
():
():
():
():
():
():
():
():
():
(): __builtin_ctz:
():
():
():
():
():
():
():
():
():
():
():
(): __builtin_popcount:
():
():
():
():
():
():
():
():
():
():
():
(): __builtin_parity:
():
():
():
():
():
():
():
():
():
():
():
(): Process returned (0x0) execution time : 2.405 s
Press any key to continue.
这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。
答:为了性能,这些函数都是内联的。
比如很多平台都有特定的指令,所以这些“函数”都只要一条指令就完成了,做成函数得不偿失。
转自:https://www.cnblogs.com/nysanier/archive/2011/04/19/2020778.html
glibc的几个有用的处理二进制位的内置函数(转)的更多相关文章
- python之有用的3个内置函数(filter/map/reduce)
这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...
- Mysql一个非常有用的内置函数今天碰到要把MySQL数据库中的varchar转换成date类型进
Mysql一个非常有用的内置函数 今天碰到要把MySQL数据库中的varchar转换成date类型进行时间的比较和查询.在网上找了找,发现MySQL也跟其他数据库一样有自己内置的转换函数:str_to ...
- Python有用的内置函数divmod,id,sorted,enumerate,input,oct,eval,exec,isinstance,ord,chr,filter,vars,zip
divmod(a, b) 函数接收两个数字类型(非复数)参数,返回一个包含商和余数的元组(a // b, a % b) id() 函数用于获取对象的内存地址. sorted(iterable, key ...
- ipython, 一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数
一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数. 若用的是fish s ...
- python装饰器内获取函数有用信息方法
装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...
- 有用的内置Node.js APIs
前言 在构建你的第一个Node.js应用程序时,了解node开箱即用的实用工具和API是很有帮助的,可以帮助解决常见的用例和开发需求. 有用的Node.js APIs Process:检索有关环境变量 ...
- 一个可能有用的封闭PGSQL操作的PYTHON函数
URL: http://www.linuxyw.com/517.html 一般操作: import psycopg2 连接数据库 conn = psycopg2.connect(database=db ...
- python 学习笔记 9 -- Python强大的自省简析
1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:“见贤思齐焉,见不贤而内自省也.”(<论语·里仁>)当然,我们今天不 ...
- Python强大的自省简析
1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:"见贤思齐焉,见不贤而内自省也."(<论语·里仁> ...
随机推荐
- Non-Inverting Level Shifter : +/-5V signal into a 0 to 3.3V
http://electronicdesign.com/boards/non-inverting-level-shifter-requires-only-one-op-amp-one-supply-v ...
- UVa409_Excuses, Excuses!(小白书字符串专题)
解题报告 题意: 找包括单词最多的串.有多个按顺序输出 思路: 字典树爆. #include <cstdio> #include <cstring> #include < ...
- 1、Cocos2dx 3.0游戏开发找小三之前言篇
尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 前言 Cocos2d-x 是一个通用 ...
- C#程序集系列02,使用记事本查看可执行程序集的IL代码
继续上一篇"C#程序集系列01,用记事本编写C#,IL代码,用DOS命令编译程序集,运行程序",在F盘的as文件夹中已经有了若干程序集.本篇体验使用记事本查看可执行程序集的IL代码 ...
- 【spring cloud】在spring cloud服务中,打包ms-core失败,报错Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) on project
在spring cloud服务中,有一个ms-code项目,只为所有的微服务提供核心依赖和工具类,没有业务意义,作为核心依赖使用.所以没有main方法,没有启动类. 在spring cloud整体打包 ...
- struts2怎么实现页面到页面之间的传值?
我要实现一个产品订购的功能,在浏览产品的时候通过点击一个订购的链接,跳转到提交订单的页面,在跳转的同时要把浏览的产品的名称和型号传到提交订单的页面,并且把这里的订单类的产品名称和型号的表单域里赋上传递 ...
- VC调用QT的UIDLL
//VC程序#include "../QTDLL/ExportDll.h" int _tmain(int argc, _TCHAR* argv[]) { printf(" ...
- 解析XML并将信息封装到对象中
[person.xml]要解析的内容 <?xml version="1.0" encoding="UTF-8"?> <students> ...
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
已測试环境: CentOS 6.3 32bit CentOS 6.5 64bit 以前在CentOS 6.3 32bit安装过OpenCV,參见CentOS 6.3中安装OpenCV2.3.1,现在换 ...
- MFC COM调用时出现E_OUTOFMEMORY错误
按照<com原理与应用>第五章写的基于MFC dll的COM,COM对象不是基于Automation的,自己映射了接口,也把潘爱民的源代码看了,感觉和他的代码一样呀,为什么在客户端用CoC ...