Builtin function

Builtin function发现Builtinfunction可以用来计算十进制数中1的个数;返回1的个数是奇数还是偶数;返回开头0的个数;返回结尾0的个数。参考链接://Cprogramtoillustrate_builtin_popcount(x)#include<stdio.h>intmain(){intn=5;…

大家好,欢迎来到IT知识分享网。

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

参考链接:

Builtin function

Builtin function

// 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; 
} 

Builtin function

// 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 function

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

Builtin function

// 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; 
} 

Builtin function

// 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; 
} 

Builtin function

// 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\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; 
} 

Builtin function

 

转载于:https://www.cnblogs.com/Bella2017/p/11282035.html

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/11533.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信