清明雨上mp3微盘下载:glibc的几个有用的处理二进制位的内置函数
来源:百度文库 编辑:偶看新闻 时间:2024/04/30 09:51:45
glibc的几个有用的处理二进制位的内置函数
作者:F.N. 来源:博客园 发布时间:2011-04-19 13:06 阅读:170 次 原文链接 [收藏] — 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
#include
#include
#include
#include
using
namespace
std;
uint32_t g_arr[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, UINT_MAX-1, 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[0].c_str());
for
(
int
i = 0; i < 12; i ++) {
int
t = g_arr[i];
bitset<32> b(t);
printf
(
"%u(%s): %d\n"
, t, b.to_string().c_str(), __builtin_ffs(t));
}
puts
(
""
);
// clz
printf
(
"%s:\n"
, g_str_func[1].c_str());
for
(
int
i = 0; i < 12; i ++) {
int
t = g_arr[i];
bitset<32> b(t);
printf
(
"%u(%s): %d\n"
, t, b.to_string().c_str(), __builtin_clz(t));
}
puts
(
""
);
// ctz
printf
(
"%s:\n"
, g_str_func[2].c_str());
for
(
int
i = 0; i < 12; i ++) {
int
t = g_arr[i];
bitset<32> b(t);
printf
(
"%u(%s): %d\n"
, t, b.to_string().c_str(), __builtin_ctz(t));
}
puts
(
""
);
// popcount
printf
(
"%s:\n"
, g_str_func[3].c_str());
for
(
int
i = 0; i < 12; i ++) {
int
t = g_arr[i];
bitset<32> b(t);
printf
(
"%u(%s): %d\n"
, t, b.to_string().c_str(), __builtin_popcount(t));
}
puts
(
""
);
// parity
printf
(
"%s:\n"
, g_str_func[4].c_str());
for
(
int
i = 0; i < 12; i ++) {
int
t = g_arr[i];
bitset<32> b(t);
printf
(
"%u(%s): %d\n"
, t, b.to_string().c_str(), __builtin_parity(t));
}
puts
(
""
);
return
0;
}
这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。 【测试结果】view source print?__builtin_ffs:
0(00000000000000000000000000000000): 0
1(00000000000000000000000000000001): 1
2(00000000000000000000000000000010): 2
3(00000000000000000000000000000011): 1
4(00000000000000000000000000000100): 3
5(00000000000000000000000000000101): 1
6(00000000000000000000000000000110): 2
7(00000000000000000000000000000111): 1
8(00000000000000000000000000001000): 4
9(00000000000000000000000000001001): 1
4294967294(11111111111111111111111111111110): 2
4294967295(11111111111111111111111111111111): 1
__builtin_clz:
0(00000000000000000000000000000000): 31
1(00000000000000000000000000000001): 31
2(00000000000000000000000000000010): 30
3(00000000000000000000000000000011): 30
4(00000000000000000000000000000100): 29
5(00000000000000000000000000000101): 29
6(00000000000000000000000000000110): 29
7(00000000000000000000000000000111): 29
8(00000000000000000000000000001000): 28
9(00000000000000000000000000001001): 28
4294967294(11111111111111111111111111111110): 0
4294967295(11111111111111111111111111111111): 0
__builtin_ctz:
0(00000000000000000000000000000000): 0
1(00000000000000000000000000000001): 0
2(00000000000000000000000000000010): 1
3(00000000000000000000000000000011): 0
4(00000000000000000000000000000100): 2
5(00000000000000000000000000000101): 0
6(00000000000000000000000000000110): 1
7(00000000000000000000000000000111): 0
8(00000000000000000000000000001000): 3
9(00000000000000000000000000001001): 0
4294967294(11111111111111111111111111111110): 1
4294967295(11111111111111111111111111111111): 0
__builtin_popcount:
0(00000000000000000000000000000000): 0
1(00000000000000000000000000000001): 1
2(00000000000000000000000000000010): 1
3(00000000000000000000000000000011): 2
4(00000000000000000000000000000100): 1
5(00000000000000000000000000000101): 2
6(00000000000000000000000000000110): 2
7(00000000000000000000000000000111): 3
8(00000000000000000000000000001000): 1
9(00000000000000000000000000001001): 2
4294967294(11111111111111111111111111111110): 31
4294967295(11111111111111111111111111111111): 32
__builtin_parity:
0(00000000000000000000000000000000): 0
1(00000000000000000000000000000001): 1
2(00000000000000000000000000000010): 1
3(00000000000000000000000000000011): 0
4(00000000000000000000000000000100): 1
5(00000000000000000000000000000101): 0
6(00000000000000000000000000000110): 0
7(00000000000000000000000000000111): 1
8(00000000000000000000000000001000): 1
9(00000000000000000000000000001001): 0
4294967294(11111111111111111111111111111110): 1
4294967295(11111111111111111111111111111111): 0
Process returned 0 (0x0) execution
time
: 2.405 s
Press any key to
continue
.
一个字节由相邻的几个二进制位组成
介绍几个内置电池的移动音箱
有用过OB内置卫生棉的吗?感觉怎么样?好用吗?
哪里有for linux的glibc-2.3.4过更高版本的rpm包下载啊
16个二进制位可表示整数的范围是
计算机中,一个字节的是()个二进制位组成?
字,位,字节,bit,二进制位的意思和关系
一个字符的标准ASCII用几位二进制位表示?
一个字节由相邻的 ( ) 个二进制位组成。
三个二进制位最多可以表示不同的状态数是
三个二进制位最多可以表示不同的状态数是
在计算机中,一个字节所包含二进制位的个数
哪里有用Photoshop处理数码照片的教程
笔记本的内置光驱的“内置”是什么意思?
河南的朋友有用ADSL内置路由共享上网成功的吗?
有用过三星X708的吗?里面的内置游戏在哪里?
笔记本的内置调制解调器
诺基亚的内置modem
内置视频的概念??
手机的内置是什么意思?
介绍几个有用的电脑软件!!
推荐几个有用的不死族战术
微机中1K字节表示的二进制位是 A 1000 B8*1000 C1024 D8*1024
用七位二进制位来表示网络号的IP属于 类地址