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

  • 相关阅读:
    Go源码文件与命令
    K8s控制器
    odoo 在form视图sheet右上角增加按钮
    odoo 常用widget
    odoo tree视图中实现横向滚动条
    可能是智障的高二生活
    千题计划
    闲谈
    线性代数与simplex
    好题集锦
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14793147.html
Copyright © 2011-2022 走看看