其他分享
首页 > 其他分享> > c 语言7-4

c 语言7-4

作者:互联网

1、

#include <stdio.h>

unsigned set(unsigned x, int pos)
{
    return x | 1U << pos;
}

unsigned reset(unsigned x, int pos)
{
    return x & (~(1U << pos));
}

unsigned inverse(unsigned x, int pos)
{
    if(x >> pos & 1U)
        return(x & (~(1U << pos)));
    else
        return(x | 1U << pos);
}

int main(void)
{
    unsigned x; int pos;
    puts("please input the test number and move bits.");
    printf("x = "); scanf("%u", &x);
    printf("pos = "); scanf("%d", &pos);
    
    printf("set    1      = %u\n",     set(x, pos));
    printf("set    0      = %u\n",   reset(x, pos));
    printf("set inverse   = %u\n", inverse(x, pos));
    
    return 0;
}

 

 

 

2、

#include <stdio.h>

unsigned set(unsigned x, int pos)
{
    return x | 1U << pos;
}

unsigned reset(unsigned x, int pos)
{
    return x & ~(1U << pos);
}

unsigned inverse(unsigned x, int pos)
{
    return x ^ 1U << pos;    
} 

int main(void)
{
    unsigned x; int pos;
    puts("please input the test number and move bits.");
    printf("x = "); scanf("%u", &x);
    printf("pos = "); scanf("%d", &pos);
    
    printf("set      1      = %u\n",    set(x, pos));
    printf("reset    0      = %u\n",  reset(x, pos));
    printf("inverse         = %u\n",inverse(x, pos));
    
    return 0;
}

 

标签:set,return,语言,int,pos,unsigned,1U
来源: https://www.cnblogs.com/liujiaxin2018/p/14793147.html