Advertisement
Josif_tepe

Untitled

Jul 4th, 2022
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <stack>
  6. #include <fstream>
  7. using namespace std;
  8. typedef long long ll;
  9. string to_binary(long long x) {
  10.     string r = "";
  11.     while(x) {
  12.         r += (x % 2) + '0';
  13.         x /= 2;
  14.     }
  15.     reverse(r.begin(), r.end());
  16.     return r;
  17. }
  18. int main() {
  19.     ll numa, numb;
  20.     int n;
  21.     cin >> n;
  22.    
  23.     for(int i = 0; i < n; i++) {
  24.         cin >> numa >> numb;
  25.         string a = to_binary(numa);
  26.         string b = to_binary(numb);
  27.        
  28.         if(a.size() != b.size()) {
  29.             cout << 0 << endl;
  30.             continue;
  31.         }
  32.         string tmp = "";
  33.         for(int i = 0; i < a.size(); i++) {
  34.             if(a[i] == b[i]) {
  35.                 tmp += a[i];
  36.             }
  37.             else {
  38.                 break;
  39.             }
  40.         }
  41.         while(tmp.size() < a.size()) {
  42.             tmp += "0";
  43.         }
  44.         long long result = 0;
  45.         long long stepen = (int) a.size() - 1;
  46.         for(int j = 0; j < tmp.size(); j++) {
  47.             if(tmp[j] == '1') {
  48.                 result |= (1LL << stepen);
  49.             }
  50.             stepen--;
  51.         }
  52.         cout << result << endl;
  53.     }
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement