— Built-in Function: int __builtin_ffs (unsigned int x)

Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.

返回右起第一个‘1’的位置。

— 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.

返回左起第一个‘1’之前0的个数。

— 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.

返回右起第一个‘1’之后的0的个数。

— Built-in Function: int __builtin_popcount (unsigned int x)

Returns the number of 1-bits in x.

返回‘1’的个数。

— 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.

返回‘1’的个数的奇偶性。

— 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的几个有用的处理二进制位的内置函数(转)的更多相关文章

  1. python之有用的3个内置函数(filter/map/reduce)

    这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...

  2. Mysql一个非常有用的内置函数今天碰到要把MySQL数据库中的varchar转换成date类型进

    Mysql一个非常有用的内置函数 今天碰到要把MySQL数据库中的varchar转换成date类型进行时间的比较和查询.在网上找了找,发现MySQL也跟其他数据库一样有自己内置的转换函数:str_to ...

  3. 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 ...

  4. ipython, 一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数

    一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数. 若用的是fish s ...

  5. python装饰器内获取函数有用信息方法

    装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...

  6. 有用的内置Node.js APIs

    前言 在构建你的第一个Node.js应用程序时,了解node开箱即用的实用工具和API是很有帮助的,可以帮助解决常见的用例和开发需求. 有用的Node.js APIs Process:检索有关环境变量 ...

  7. 一个可能有用的封闭PGSQL操作的PYTHON函数

    URL: http://www.linuxyw.com/517.html 一般操作: import psycopg2 连接数据库 conn = psycopg2.connect(database=db ...

  8. python 学习笔记 9 -- Python强大的自省简析

    1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:“见贤思齐焉,见不贤而内自省也.”(<论语·里仁>)当然,我们今天不 ...

  9. Python强大的自省简析

    1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:"见贤思齐焉,见不贤而内自省也."(<论语·里仁> ...

随机推荐

  1. Microsoft Composition (MEF 2)

    This packages provides a version of the Managed Extensibility Framework (MEF) that is lightweight an ...

  2. 转 ios给view设置圆角

    // 圆角 userhead.layer.masksToBounds = YES; userhead.layer.cornerRadius = 6.0; userhead.layer.borderWi ...

  3. list去除重复数据

    在java里面要想去除list中的重复数据可以使用两种方式实现: 1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) ...

  4. 对JVM还一知半解

    对JVM还一知半解?这篇文章让你彻底搞定JVM 摘要: 对于Java开发者来说,想把自身能力提升到更高层次,某些JVM相关知识应该是优先级很高的.比如说GC策略,JVM调优. 就我在工作中遇到的情况来 ...

  5. Mongodb安全认证及Java调用

    Mongodb安全认证在单实例和副本集两种情况下不太一样,单实例相对简单,只要在启动时加上 --auth参数即可,但副本集则需要keyfile. 一.单实例 1.启动服务(先不要加auth参数) 2. ...

  6. Java数据库编程技术

    1. 建立数据库连接 例1.1 使用JDBC-ODBC桥来连接一个Access数据库. 该数据库的名称为FirstExample,在ODBC数据源中的名称为forStudy,用户名和密码均为空. pa ...

  7. HTTP参数CONNETCTION_TIMEOUT和SO_TIMEOUT区别

    在开发中经常碰到这两个参数,但是之前对它们的真正含义一直比较模糊,今天通过调试程序并且结合官方文档,了解了两者的含义与区别. 参数的定义直接去看官方的文档(httpcore-4.3) org.apac ...

  8. State 状态模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. maven mvn Failed during checkstyle execution

    maven 命令默认强制使用checkstyle, 造成 命令运行失败 并报错: Failed during checkstyle execution 关闭checkstyle,命令如下: mvn [ ...

  10. 两个JS对象怎样才能相等

    在JS中,两个对象如何才能相等?下面的两个 Alert,只有一个输出true.  在JS中如何才能构造出两个JS对象相等? var prop1 = {asd:{def:'abc'}}; var pro ...