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.
1 #include <stdio.h> 2 #include <iostream> 3 #include <bitset> 4 #include <string> 5 #include <limits.h> 6 7 using namespace std; 8 9 uint32_t g_arr[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, UINT_MAX-1, UINT_MAX}; 10 11 string g_str_func[] = { 12 "__builtin_ffs", 13 "__builtin_clz", 14 "__builtin_ctz", 15 "__builtin_popcount", 16 "__builtin_parity" 17 }; 18 19 //typedef int (*fp_builtin_t)(unsigned int); 20 21 //fp_builtin_t g_func[] = { 22 //__builtin_ffs, 23 //__builtin_clz, 24 //__builtin_ctz, 25 //__builtin_popcount, 26 //__builtin_parity 27 //}; 28 29 int main() 30 { 31 /* for (int k = 0; k < 5; k ++) { 32 printf("%s:\n", g_str_func[k].c_str()); 33 for (int i = 0; i < 12; i ++) { 34 int t = g_arr[i]; 35 bitset<32> b(t); 36 fp_builtin_t fp_func = g_func[k]; 37 printf("%u(%s): %d\n", t, b.to_string().c_str(), fp_func(t)); 38 } 39 40 puts(""); 41 } 42 */ 43 44 // ffs 45 printf("%s:\n", g_str_func[0].c_str()); 46 for (int i = 0; i < 12; i ++) { 47 int t = g_arr[i]; 48 bitset<32> b(t); 49 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ffs(t)); 50 } 51 puts(""); 52 53 // clz 54 printf("%s:\n", g_str_func[1].c_str()); 55 for (int i = 0; i < 12; i ++) { 56 int t = g_arr[i]; 57 bitset<32> b(t); 58 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_clz(t)); 59 } 60 puts(""); 61 62 // ctz 63 printf("%s:\n", g_str_func[2].c_str()); 64 for (int i = 0; i < 12; i ++) { 65 int t = g_arr[i]; 66 bitset<32> b(t); 67 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ctz(t)); 68 } 69 puts(""); 70 71 // popcount 72 printf("%s:\n", g_str_func[3].c_str()); 73 for (int i = 0; i < 12; i ++) { 74 int t = g_arr[i]; 75 bitset<32> b(t); 76 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_popcount(t)); 77 } 78 puts(""); 79 80 // parity 81 printf("%s:\n", g_str_func[4].c_str()); 82 for (int i = 0; i < 12; i ++) { 83 int t = g_arr[i]; 84 bitset<32> b(t); 85 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_parity(t)); 86 } 87 puts(""); 88 return 0; 89 }
【测试结果】
1 __builtin_ffs: 2 0(00000000000000000000000000000000): 0 3 1(00000000000000000000000000000001): 1 4 2(00000000000000000000000000000010): 2 5 3(00000000000000000000000000000011): 1 6 4(00000000000000000000000000000100): 3 7 5(00000000000000000000000000000101): 1 8 6(00000000000000000000000000000110): 2 9 7(00000000000000000000000000000111): 1 10 8(00000000000000000000000000001000): 4 11 9(00000000000000000000000000001001): 1 12 4294967294(11111111111111111111111111111110): 2 13 4294967295(11111111111111111111111111111111): 1 14 15 __builtin_clz: 16 0(00000000000000000000000000000000): 31 17 1(00000000000000000000000000000001): 31 18 2(00000000000000000000000000000010): 30 19 3(00000000000000000000000000000011): 30 20 4(00000000000000000000000000000100): 29 21 5(00000000000000000000000000000101): 29 22 6(00000000000000000000000000000110): 29 23 7(00000000000000000000000000000111): 29 24 8(00000000000000000000000000001000): 28 25 9(00000000000000000000000000001001): 28 26 4294967294(11111111111111111111111111111110): 0 27 4294967295(11111111111111111111111111111111): 0 28 29 __builtin_ctz: 30 0(00000000000000000000000000000000): 0 31 1(00000000000000000000000000000001): 0 32 2(00000000000000000000000000000010): 1 33 3(00000000000000000000000000000011): 0 34 4(00000000000000000000000000000100): 2 35 5(00000000000000000000000000000101): 0 36 6(00000000000000000000000000000110): 1 37 7(00000000000000000000000000000111): 0 38 8(00000000000000000000000000001000): 3 39 9(00000000000000000000000000001001): 0 40 4294967294(11111111111111111111111111111110): 1 41 4294967295(11111111111111111111111111111111): 0 42 43 __builtin_popcount: 44 0(00000000000000000000000000000000): 0 45 1(00000000000000000000000000000001): 1 46 2(00000000000000000000000000000010): 1 47 3(00000000000000000000000000000011): 2 48 4(00000000000000000000000000000100): 1 49 5(00000000000000000000000000000101): 2 50 6(00000000000000000000000000000110): 2 51 7(00000000000000000000000000000111): 3 52 8(00000000000000000000000000001000): 1 53 9(00000000000000000000000000001001): 2 54 4294967294(11111111111111111111111111111110): 31 55 4294967295(11111111111111111111111111111111): 32 56 57 __builtin_parity: 58 0(00000000000000000000000000000000): 0 59 1(00000000000000000000000000000001): 1 60 2(00000000000000000000000000000010): 1 61 3(00000000000000000000000000000011): 0 62 4(00000000000000000000000000000100): 1 63 5(00000000000000000000000000000101): 0 64 6(00000000000000000000000000000110): 0 65 7(00000000000000000000000000000111): 1 66 8(00000000000000000000000000001000): 1 67 9(00000000000000000000000000001001): 0 68 4294967294(11111111111111111111111111111110): 1 69 4294967295(11111111111111111111111111111111): 0 70 71 72 Process returned 0 (0x0) execution time : 2.405 s 73 Press any key to continue.
这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。
答:为了性能,这些函数都是内联的。
比如很多平台都有特定的指令,所以这些“函数”都只要一条指令就完成了,做成函数得不偿失。
转自:https://www.cnblogs.com/nysanier/archive/2011/04/19/2020778.html
转载于:https://www.cnblogs.com/zl1991/p/8192509.html
glibc的几个有用的处理二进制位的内置函数(转)相关推荐
- python func函数用法_python教程:3个非常有用的内置函数
这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...
- [收藏]编译器内置的一些有用的调试宏
函数名,函数签名... __FUNCSIG__ __FUNCDNAME__ __FUNCTION__ __func__ __FUNCSIG__ __FUNCDNAME__ __FUNCTION__ _ ...
- 零基础入门学习Python(20)-lambda表达式、filter()、map() BIF
lambda表达式 lambda关键字的使用 Python允许使用lambda关键字来创建匿名函数 基本语法:使用冒号(:)分隔函数的参数及返回值,冒号左边放置函数的参数,如果有多个参数,使用逗号(, ...
- Python学习之zip函数
Python 学习之 zip 函数 问题的引出 有时候,你可能想同时迭代两个序列.假设有下面两个列表: names = ['anne', 'beth', 'george', 'damon'] ages ...
- python基础----文件处理
一.文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 1 正趣果上果 2 Interesting fruit fruit 3 4 词:郭婞 5 曲:陈粒 6 编曲 ...
- Python循环流程
1.for循环 计算1+2+3+--+100的和 1 count = 0 2 i = 1 3 for i in range(101): 4 count+=i 5 print(count) 前n项和公式 ...
- PHP培训教程 PHP里10个鲜为人知但却非常有用的函数
php里有非常丰富的内置函数,很多我们都用过,但仍有很多的函数我们大部分人都不熟悉,可它们却十分的有用.这篇文章里,兄弟连小编列举了一些鲜为人知但会让你眼睛一亮的PHP函数. levenshtein( ...
- PHP里10个鲜为人知但却非常有用的函数
PHP里有非常丰富的内置函数,很多我们都用过,但仍有很多的函数我们大部分人都不熟悉,可它们却十分的有用.这篇文章里,我列举了一些鲜为人知但会让你眼睛一亮的PHP函数. levenshtein() 你有 ...
- 一系列有用的Python技巧
Python作为世界上最受欢迎的编程语言之一.这有很多原因: 易于学习 超级通用 具有大量的模块和库 本文将尝试以A-Z的顺序分享其中一些有用的技巧和窍门.这些"窍门"大部分是我在 ...
- 你需要知道的13个有用的Python片段
你需要知道的13个有用的Python片段 用这些日常片段优化你的工作流程 Python是当今广泛使用的编程语言之一,应用于各个领域,如数据科学.科学计算.网络开发.游戏开发和构建桌面图形界面.Pyth ...
最新文章
- 使用超图在网页上浏览地形
- 【LaTeX】E喵的LaTeX新手入门教程(2)基础排版
- 电气期刊论文实现:基于改进遗传算法的电力机组组合(程序讲解)
- C#中DateTime.Ticks属性及Unix时间戳转换
- java web开发常见问题_JavaWeb学习笔记(五)--Web开发其他常见问题
- J2EE中EL表达式
- python 散点图点击链接图片_在Python和matplotlib中连接三维散点图中的两点
- c语言 一个矩阵的乘积,c语言矩阵相乘
- 点击按钮后网页不向上滚动
- 哪一类功率放大电路效率最高_集成电路工艺之双极型工艺
- 每天一点正则表达式积累之(?=X)和(?!X)测试(七)
- 易进难出,“Vim 退出” 难住百万程序员
- java int转byte出现负数 byte转int的解决
- 计算机word图标不显示,Win7系统中Word文档图标无法正常显示怎么办?
- Spark Streaming系列-1、什么是Spark Streaming?
- 阿里云服务器购买搭建过程
- 最新Kafka教程(包含kafka部署与基本操作、java连接kafka、spring连接kafka以及使用springboot)
- 【问链-EOS公开课】第十五课 用cleos注册EOS主网账户、投票和发币
- Qt在 Mac系统发布程序(widgets和quick2)生成dmg安装文件。
- VUE每次请求Session丢失
热门文章
- 2021-08-08 idea 连接Mysql
- python生活中的小问题_python日常注意小知识集锦
- word页码怎么从第三页开始设置为第一页_如何让页码从指定页开始,而不是第一页?...
- 简述python文件操作_python 文件操作总结
- scp ssh: connect to host 192.168.6.129 port 22: Connection refused lost connection
- js 实现PHP中的in_array()
- c语言中ox1小于小于a,丹江口市2018适应性数学试卷和答案
- vue 插值表达式,v-cloak,v-text,v-html,以及v-bind,v-on
- 关于 in与exist , not in与not exist 的区别
- springboot 微服务相关收藏