c语言 7-4

编写reverse函数,返回将无符号整数x的第pos位取反后的值。

1、

#include <stdio.h>

unsigned reverse(unsigned x, int pos)
{
    if(x >> pos & 1U)
        return (x & (~(1 << pos)));
    else
        return (x | 1 << pos);
}

int main(void)
{
    unsigned x;
    int n;
    puts("please input two nonnegatives.");
    printf("x = "); scanf("%u", &x);
    printf("n = "); scanf("%d", &n);
    
    printf("result: %u
", reverse(x, n));
    return 0;
}

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14786099.html