glibc的几个有用的处理二进制位的内置函数
http://www.cnblogs.com/nysanier/archive/2011/04/19/2020778.html
— 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.
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.
— 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.
— 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.
— 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.
— 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.
— 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[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;} |
这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。
【测试结果】
__builtin_ffs:0(00000000000000000000000000000000): 01(00000000000000000000000000000001): 12(00000000000000000000000000000010): 23(00000000000000000000000000000011): 14(00000000000000000000000000000100): 35(00000000000000000000000000000101): 16(00000000000000000000000000000110): 27(00000000000000000000000000000111): 18(00000000000000000000000000001000): 49(00000000000000000000000000001001): 14294967294(11111111111111111111111111111110): 24294967295(11111111111111111111111111111111): 1__builtin_clz:0(00000000000000000000000000000000): 311(00000000000000000000000000000001): 312(00000000000000000000000000000010): 303(00000000000000000000000000000011): 304(00000000000000000000000000000100): 295(00000000000000000000000000000101): 296(00000000000000000000000000000110): 297(00000000000000000000000000000111): 298(00000000000000000000000000001000): 289(00000000000000000000000000001001): 284294967294(11111111111111111111111111111110): 04294967295(11111111111111111111111111111111): 0__builtin_ctz:0(00000000000000000000000000000000): 01(00000000000000000000000000000001): 02(00000000000000000000000000000010): 13(00000000000000000000000000000011): 04(00000000000000000000000000000100): 25(00000000000000000000000000000101): 06(00000000000000000000000000000110): 17(00000000000000000000000000000111): 08(00000000000000000000000000001000): 39(00000000000000000000000000001001): 04294967294(11111111111111111111111111111110): 14294967295(11111111111111111111111111111111): 0__builtin_popcount:0(00000000000000000000000000000000): 01(00000000000000000000000000000001): 12(00000000000000000000000000000010): 13(00000000000000000000000000000011): 24(00000000000000000000000000000100): 15(00000000000000000000000000000101): 26(00000000000000000000000000000110): 27(00000000000000000000000000000111): 38(00000000000000000000000000001000): 19(00000000000000000000000000001001): 24294967294(11111111111111111111111111111110): 314294967295(11111111111111111111111111111111): 32__builtin_parity:0(00000000000000000000000000000000): 01(00000000000000000000000000000001): 12(00000000000000000000000000000010): 13(00000000000000000000000000000011): 04(00000000000000000000000000000100): 15(00000000000000000000000000000101): 06(00000000000000000000000000000110): 07(00000000000000000000000000000111): 18(00000000000000000000000000001000): 19(00000000000000000000000000001001): 04294967294(11111111111111111111111111111110): 14294967295(11111111111111111111111111111111): 0Process returned 0 (0x0) execution time : 2.405 sPress any key to continue. |

浙公网安备 33010602011771号