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. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:"见贤思齐焉,见不贤而内自省也."(<论语·里仁> ...
随机推荐
- ATMEL精妙的IRQ中断处理过程
A: 从栈地址开始,栈顶为AT91SAM7S64的16K片内RAM尽头0x00204000IRQ_STACK_SIZE = 3*8*4FIQ_STACK_SIZE = 0x004ABT_STACK_S ...
- adb root : adbd cannot run as root in production builds
在有些android手机上使用adb root希望获取root权限时出现如下提示信息:adbd cannot run as root in production builds.此时提升root权限的方 ...
- 74HC245 74HCT245 74LV245 74LVC245 74LVC4245A 74LVC8T245 74LVC16T245 74ALVC164245
74HC245/74HCT245 The 74HC245; 74HCT245 is a high-speed Si-gate CMOS device and is pin compatible wit ...
- C# 转换图形为PCX 格式
2010-5-27 PCX RLE压缩图形的行对齐比.NET多了一位.已经修正了. 2009 -7-25 C# 转换图形为PCX 格式 增加了对1位色的PCX的读取 2009-6 -12 RLE数据压 ...
- MVC文件上传03-使用Request.Files上传多个文件
本篇体验在控制器方法中使用controllerContext.HttpContext.Request.Files上传多个文件.兄弟篇为: MVC文件上传01-使用jquery异步上传并客户端验证类型和 ...
- Spring Boot中使用redis的发布/订阅模式
原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...
- pytest文档16-用例a失败,跳过测试用例b和c并标记失败xfail
前言 当用例a失败的时候,如果用例b和用例c都是依赖于第一个用例的结果,那可以直接跳过用例b和c的测试,直接给他标记失败xfail 用到的场景,登录是第一个用例,登录之后的操作b是第二个用例,登录之后 ...
- mac OS X下配置jdk环境变量
进入命令行,开始如下操作: cd ~touch.bash_profile vi .bash_profile 输入内容jdk变量配置内容: export JAVA_HOME=/Library/Jav ...
- Redis中对Key进行分类
使用":"体现层次 >set key1:key2:key4 value1 "OK" >set key1:key2:key5 value2 " ...
- Iterator 迭代器模式 MD
迭代器模式 简介 Iterator模式是行为模式之一,它把对容器中包含的内部对象的访问[委让]给外部类,使用Iterator按顺序进行遍历访问. 在程序设计中,经常有这种情况:需要从大量的数据集合中一 ...