zoukankan      html  css  js  c++  java
  • Builtin function

    发现 Builtin function 可以用来计算十进制数中1的个数;返回1的个数是奇数还是偶数;返回开头0的个数;返回结尾0的个数。

    参考链接:

    // C program to illustrate _builtin_popcount(x) 
    
    #include <stdio.h> 
    int main() 
    { 
        int n = 5; 
        printf("Count of 1s in binary of %d is %d ", 
            n, __builtin_popcount(n)); 
        return 0; 
    } 

    // C program to illustrate _builtin_parity(x) 
    
    #include <stdio.h> 
    int main() 
    { 
        int n = 7; 
        printf("Parity of %d is %d ", 
            n, __builtin_parity(n)); 
        return 0; 
    } 

    第二个函数 _builtin_parity(x) 是用来计算:x中1的个数是奇数个则返回1,否则返回0.

    // C program to illustrate __builtin_clz(x) 
    #include <stdio.h> 
    int main() 
    { 
        int n = 16; 
        printf("Count of leading zeros before 1 in %d is %d", 
            n, __builtin_clz(n)); 
        return 0; 
    } 

    // C program to illustrate __builtin_ctz(x) 
    #include <stdio.h> 
    int main() 
    { 
        int n = 16; 
        printf("Count of zeros from last to first "
            "occurrence of one is %d", 
            __builtin_ctz(n)); 
        return 0; 
    } 

    // C program to illustrate builtin functions of 
    // GCC compiler 
    #include <stdio.h> 
    #include <stdlib.h> 
    
    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
    ", num, pop); 
    
        parity = __builtin_parity(num); 
        printf("Parity of %d is %d
    ", num, parity); 
    
        clz = __builtin_clz(num); 
        printf("Number of leading zero's in %d is %d
    ", num, clz); 
    
        // It only works for unsigned values 
        clz = __builtin_clz(-num); 
        printf("Number of leading zero's in %d is %d
    ", -num, clz); 
    
        ctz = __builtin_ctz(num); 
        printf("Number of trailing zero's in %d is %d
    ", num, ctz); 
    
        return 0; 
    } 

  • 相关阅读:
    python 二维数组遍历
    WINFORM中treeview 节点显示不全
    C++函数式编程实现牛顿法
    C++函数式编程
    C++函数的重载
    默认形参值
    常量指针和指针常量
    C++值传递与引用传递
    C++变量和基本类型——2.3.1引用
    C++ 实参和形参
  • 原文地址:https://www.cnblogs.com/Bella2017/p/11282035.html
Copyright © 2011-2022 走看看