zoukankan      html  css  js  c++  java
  • 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
    ",     set(x, pos));
        printf("set    0      = %u
    ",   reset(x, pos));
        printf("set inverse   = %u
    ", 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
    ",    set(x, pos));
        printf("reset    0      = %u
    ",  reset(x, pos));
        printf("inverse         = %u
    ",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
    ", set(x, pos));
        printf("reset     0     = %u
    ", reset(x, pos));
        printf("inverse         = %u
    ", inverse(x, pos));
        
        return 0;
    }

  • 相关阅读:
    快速构建一个权限项目(七)
    快速构建一个权限项目(六)
    快速构建一个权限项目(五)
    快速构建一个权限项目(四)
    快速构建一个权限项目(三)
    快速构建一个权限项目(二)
    快速构建一个权限项目(一)
    MySql快速入门(四)
    Mysql快速入门(三)
    Mysql快速入门(二)
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14793147.html
Copyright © 2011-2022 走看看