位运算例题4

    xiaoxiao2021-03-26  24

    1. 题目:输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果.

    #include<stdio.h> int main() { unsigned int num,temp1,temp2,temp3,temp4,k,num1,num2; int i = 0; printf("Please enter an integer:\n"); scanf("%d",&num); temp1 = num; temp2 = num; //分别向左右移一位再异或,即可得到相邻两位异或的结果 temp1 <<= 1; //左移一位在加上溢出的 temp2 >>= 31; num1 = temp1 + temp2; temp3 = num; temp4 = num; temp3 >>= 1; //右移一位在加上舍弃的 temp4 <<= 31; num2 = temp3 + temp4; num = num1 ^ num2; //相邻两位异或 printf("%ud",num); }
    转载请注明原文地址: https://ju.6miu.com/read-661761.html

    最新回复(0)