• c语言 7-5


    编写inverse_n函数,返回将无符号整数x的第pos为开始的n位取反后的值。

    1、

    #include <stdio.h>
    
    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 & (~(1 << i)));
            else
                x = (x | 1 << i);
        }
        return x;
    }
    
    int main(void)
    {
        unsigned x;
        int pos, i;
        puts("please input three integers.");
        printf("x = "); scanf("%u", &x);
        printf("pos = "); scanf("%d", &pos);
        printf("i = "); scanf("%d", &i);
        
        printf("result: %u
    ", inverse_n(x, pos, i));
        return 0;
    }

  • 相关阅读:
    ajax01
    django04
    数据库
    WeakHashMap类
    IdentityHashMap
    Hashtable类
    LinkedHashMap类
    HashMap和TreeMap类
    PriorityQueue
    Synchronized
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14786586.html
Copyright © 2020-2023  润新知