首页 > 编程语言 > 详细

c语言 7-5

时间:2021-05-21 14:27:02      阅读:24      评论:0      收藏:0      [点我收藏+]

1、

#include <stdio.h>

unsigned set_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x | 1U << i;
    }
    return x;
}

unsigned reset_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x & ~(1U << i);
    }
    return x;
}

unsigned inverse_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x ^ 1U << i;
    }
    return x;
}

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

技术分享图片

 

 

2、

#include <stdio.h>

unsigned set_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x | 1U << i;
    }
    return x;
}

unsigned reset_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x & ~(1U << i);
    }
    return x;
}

unsigned inverse_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        if(x >> i & 1U)
            x = x & ~(1U << i);
        else
            x = x | 1U << i;
    }
    return x;
}

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

技术分享图片

 

3、

#include <stdio.h>

unsigned set_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x | 1U << i;
    }
    return x;
}

unsigned reset_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        if(x & 1U << i)
            x = x ^ 1U << i;
        else
            x = x;
    }
    return x;
}

unsigned inverse_n(unsigned x, int pos, int n)
{
    int i;
    for(i = pos; i <= pos + n - 1; i++)
    {
        x = x ^ 1U << i;
    }
    return x;
}

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

技术分享图片

 

c语言 7-5

原文:https://www.cnblogs.com/liujiaxin2018/p/14793321.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!