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

 

3、

#include <stdio.h>

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

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

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

 

posted @ 2021-05-21 11:44  小鲨鱼2018  阅读(74)  评论(0编辑  收藏  举报