📜  GCC编译器的内置函数

📅  最后修改于: 2021-05-25 01:30:57             🧑  作者: Mango

这些是GCC编译器中的四个重要的内置函数:

  1. _builtin_popcount(x):此函数用于对一个整数(设置位)的个数进行计数。
    例子:
    if x = 4
    binary value of 4 is 100
    Output: No of ones is 1.
    // C program to illustrate _builtin_popcount(x)
      
    #include 
    int main()
    {
        int n = 5;
          
        printf("Count of 1s in binary of %d is %d ",
               n, __builtin_popcount(n));
        return 0;
    }
    
    输出:
    Count of 1s in binary of 5 is 2
    

    注意:同样,对于长数据类型和长长数据类型,您可以使用__builtin_popcountl(x)和__builtin_popcountll(x)。

  2. _builtin_parity(x):此函数用于检查数字的奇偶校验。如果数字具有奇偶校验,则此函数返回true(1),否则返回奇偶校验的false(0)。
    例子:
    if x = 7
    7 has odd no. of 1's in its binary(111).
    Output: Parity of 7 is 1 
    // C program to illustrate _builtin_parity(x)
      
    #include 
    int main()
    {
        int n = 7;
          
        printf("Parity of %d is %d ",
               n, __builtin_parity(n));
        return 0;
    }
    
    输出:
    Parity of 7 is 1
    

    注意:同样,对于长数据类型和长长数据类型,您可以使用__builtin_parityl(x)和__builtin_parityll(x)。

  3. __builtin_clz(x):此函数用于计算整数的前导零。注意:clz =计数前导零
    示例:它在第一次出现一个(置位)之前对零的数目进行计数。
    a = 16
    Binary form of 16 is 00000000 00000000 00000000 00010000
    Output: 27
    // C program to illustrate __builtin_clz(x)
    #include 
    int main()
    {
        int n = 16;
          
        printf("Count of leading zeros before 1 in %d is %d",
               n, __builtin_clz(n));
        return 0;
    }
    
    输出:
    Count of leading zeros before 1 in 16 is 27
    

    注意: __builtin_clz(x)此函数仅接受无符号值
    注意:同样,对于长数据类型和长长数据类型,您可以使用__builtin_clzl(x)和__builtin_clzll(x)。

  4. __builtin_ctz(x):此函数用于计算给定整数的尾随零。注意:ctz =计数尾随零。
    示例:从上次出现到第一次出现(置位)时不计零。
    a = 16
    Binary form of 16 is 00000000 00000000 00000000 00010000
    Output: ctz = 4
    
    // C program to illustrate __builtin_ctz(x)
    #include 
    int main()
    {
        int n = 16;
          
        printf("Count of zeros from last to first "
               "occurrence of one is %d",
               __builtin_ctz(n));
        return 0;
    }
    
    输出:
    Count of zeros from last to first occurrence of one is 4
    

    注意:同样,对于长数据类型和长长数据类型,您可以使用__builtin_ctzl(x)和__builtin_ctzll(x)。

    // C program to illustrate builtin functions of
    // GCC compiler
    #include 
    #include 
      
    int main()
    {
        int num = 4;
        int clz = 0;
        int ctz = 0;
        int pop = 0;
        int parity = 0;
      
        pop = __builtin_popcount(num);
        printf("Number of one's in %d is %d\n", num, pop);
      
        parity = __builtin_parity(num);
        printf("Parity of %d is %d\n", num, parity);
      
        clz = __builtin_clz(num);
        printf("Number of leading zero's in %d is %d\n", num, clz);
      
        // It only works for unsigned values
        clz = __builtin_clz(-num);
        printf("Number of leading zero's in %d is %d\n", -num, clz);
      
        ctz = __builtin_ctz(num);
        printf("Number of trailing zero's in %d is %d\n", num, ctz);
      
        return 0;
    }
    
    输出:
    Number of one's in 4 is 1
    Parity of 4 is 1
    Number of leading zero's in 4 is 29
    Number of leading zero's in -4 is 0
    Number of trailing zero's in 4 is 2
    
    要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”