zoukankan      html  css  js  c++  java
  • c语言 7-5

    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
    ", set_n(x, pos, n));
        printf("set      0      = %u
    ", reset_n(x, pos, n));
        printf("inverse         = %u
    ", 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
    ", set_n(x, pos, n));
        printf("set       1        = %u
    ", reset_n(x, pos, n));
        printf("inverse            = %u
    ", 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
    ", set_n(x, pos, n));
        printf("set      0       = %u
    ", reset_n(x, pos, n));
        printf("inverse          = %u
    ", inverse_n(x, pos, n));
        
        return 0;
    }

  • 相关阅读:
    IMWebConf 2017 官网彩蛋解谜
    解决SVG animation 在IE中不起作用
    百度大搜和度秘面经
    浅谈JavaScript原型与原型链
    听说2017你想写前端?
    如何制作icon-font小图标
    HTML5 CSS3 诱人的实例 :模仿优酷视频截图功能
    javaweb action无法跳转、表单无法跳转的解决方法
    hadoop备战:yarn框架的搭建(mapreduce2)
    liferay 指定默认首页
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14793321.html
Copyright © 2011-2022 走看看