c语言中按位逻辑运算符、位移运算符

2021-05-28 15:01

阅读:443

标签:sign   rgba   clu   har   位移运算   turn   input   col   code   

c语言中按位逻辑运算符、位移运算符

#include int count_bits(unsigned x)
{
    int bits = 0;
    while(x)
    {
        if(x & 1U)
            bits++;
        x >>= 1;
    }
    return bits;
}

int int_bits(void)
{
    return count_bits(~0U);
}

int print_bits(unsigned x)
{
    int i;
    for(i = int_bits() - 1; i >= 0; i--)
    {
        putchar(((x >> i) & 1U) ? 1 : 0);
    }
    putchar(\n);
}

int main(void)
{
    unsigned a, b;
    puts("please input two unsigned integers.");
    printf("unsigned a = "); scanf("%u", &a);
    printf("move unit  = "); scanf("%u", &b);
    
    printf("\nbinary a    = "); print_bits(a);
    printf("right move  = "); print_bits(a >> b);
    printf("left move   = "); print_bits(a  b);
    return 0;
}

技术图片

 

c语言中按位逻辑运算符、位移运算符

标签:sign   rgba   clu   har   位移运算   turn   input   col   code   

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14780434.html


评论


亲,登录后才可以留言!