Ankit_132

Minimize Xor Difference

Sep 20th, 2023
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7.  
  8. int main()
  9. {
  10.     _test
  11.     {
  12.         int a, b;
  13.         cin>>a>>b;
  14.  
  15.         if(a == b)
  16.         {
  17.             cout<<0<<"\n";
  18.             continue;
  19.         }
  20.  
  21.         if(a<b)     swap(a, b);
  22.  
  23.         int bit = -1;
  24.  
  25.         for(int i=29; i>=0; i--)
  26.         {
  27.             int x = (1<<i);
  28.  
  29.             if((a&x) != (b&x))
  30.             {
  31.                 bit = i;
  32.                 break;
  33.             }
  34.         }
  35.  
  36.         int ans = 0;
  37.  
  38.         for(int i=bit-1; i>=0; i--)
  39.         {
  40.             int x = (1<<i);
  41.  
  42.             if((a&x)>0 && (b&x)==0)
  43.                 ans += x;
  44.         }
  45.  
  46.         cout<<ans<<"\n";
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment