位运算

https://xwmm.github.io/2019/05/23/%E4%BD%8D%E8%BF%90%E7%AE%97/

数学函数很重要 gcd,求和,ect。

/*
1:
Problem Description
Bit operation is a common computing method in computer science ,Now we have two positive integers A and B ,Please find a positive integer C that minimize the value of the formula (A  xor  C)  &  (B  xor  C) .Sometimes we can find a lot of C to do this ,So you need to find the smallest C that meets the criteria .

For example ,Let's say A is equal to 5 and B is equal to 3 ,we can choose C=1,3.... ,so the answer we're looking for C is equal to 1.
 

Input
The input file contains T test samples.(1<=T<=100)

The first line of input file is an integer T.

Then the T lines contains 2 positive integers, A and B, (1≤A,B<232)
 

Output
For each test case,you should output the answer and a line for each answer.
 

Sample Input
1
3 5
 

Sample Output
1*/
#include <iostream>
using namespace std;
int main(){
    int n,i,i0;
    unsigned long a,b,c=0,d;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a>>b;
        d=1;
        d<<=31;
        for(i0=31;i0>=0;i0--){
            c<<=1;
            if(((a|d)==a)&&((b|d)==b)){
                c|=0x01;
            }
            d>>=1;
        }
        if(c==0) cout<<1;
        else cout<<c;
        cout<<'\n';
    }
}
/*
原式 (a^c)&(b^c)
a=1,b=1时 c=0,原式=1 c=1,原式=0 
a=1,b=0 或 a=0,b=1时 c=1 或 c=0 原式=0
a=0,b=0时 c=1,原式=1 c=0,原式=0
所以想要c尽可能小就要在a=1,b=1以外的情况让c=0
只需依次运算每一位即可 
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值