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. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:"见贤思齐焉,见不贤而内自省也."(<论语·里仁> ...
随机推荐
- ThinkPHP 数据库操作之数据表模型和基础模型 ( Model )
一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: public function te ...
- 在EntityFramework6中管理DbContext的正确方式——2DbContext的默认行为(外文翻译)
(译者注:使用EF开发应用程序的一个难点就在于对其DbContext的生命周期管理,你的管理策略是否能很好的支持上层服务 使用独立事务,使用嵌套事务,并行执行,异步执行等需求? Mehdi El Gu ...
- Unity3d Http Get请求
新浪微博的OpenAPI登录 public static IEnumerator LoginRequest(string userid, string passwd, Action<string ...
- nsstring 怎么包含”(引号)号
使用转义字符 \ 即可.如下: [NSString *string = @"\"好\""]; NSLog("%@",string); 打印结 ...
- Windows上编译libtiff
将libtiff 4.0.3解压到[工作目录]/tiff/tiff-4.0.3 对于Release,编辑tiff/tiff-4.0.3里面的nmake.opt如下选项,去掉注释: JPEG_SUPPO ...
- Log4j按级别输出日志到不同文件配置分析
关于LOG4J 按照级别输出日志,并按照级别输出到不同文件中的说法有很多, 网上贴的最多的log4j.properties的设置是这样的 log4j.rootLogger=info,stdout,in ...
- Asp.Net中自以为是的Encode
Asp.Net 引擎可能是不错,但是它把程序员想的太笨,会自以为是做很多自动的 Encode 和 Decode,以下文举例: 如果客户端我们 post 了如下的数据, 但是你实际得到的是: 也就是说, ...
- vim/vi 命令详解
在工作中,要对服务器上的文件进行的修改,可以使用ssh远程登录到服务器上,并且使用vi进行快速的编辑即可,在没有图形界面的环境下,要编辑文件,vi是最佳选择! vi命令是Linux中最经典的文本编辑器 ...
- 用过Retina视网膜屏幕的笔记本电脑的后果
用过Retina视网膜屏幕的笔记本电脑的后果是过程中感觉很不错,但是结果是普通屏幕再也看不上眼了.发现了原来看的好好的屏幕多出了许多的像素点,没办法,火眼金睛了.
- 数学图形之克莱因瓶(klein bottle)
克莱因瓶是一种内外两面在同一个曲面上的图形. 在数学领域中,克莱因瓶(德语:Kleinsche Flasche)是指一种无定向性的平面,比如二维平面,就没有“内部”和“外部”之分.克莱因瓶最初的概念提 ...