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

  • 相关阅读:
    AngularJS定时器任务
    ssh常用
    HTTPClient模块的HttpGet和HttpPost
    eclipse下设置tomcat,修改Java代码不必重启tomcat
    【转】调试Release发布版程序的Crash错误
    C/C++ 函数压栈方式
    PHP 安全三板斧:过滤、验证和转义之转义篇 & Blade模板引擎避免XSS攻击原理探究
    让 MySQL 支持 emoji 存储
    Laravel 5.1 中创建自定义 Artisan 控制台命令实例教程
    常见的Web实时消息交互方式和SignalR
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14793321.html
Copyright © 2011-2022 走看看