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

  • 相关阅读:
    Linux Date命令学习笔记
    NOIP201401珠心算测验
    经典的背包问题
    简单动态规划---动态的数字三角形
    idea关于tab的设置
    idea设置字体大小
    C# 使用Queue<T>代替递归算法遍历树
    Python__用户交互,显示省市县三级联动的选择
    Python习题__购物车
    Python练习题
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14793147.html
Copyright © 2011-2022 走看看