Beingamanforever

Hackerrank Sum VS XOR

Nov 12th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-11-12 21:43:25
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     // TODO: a^b = a+b; that means carry is 0 hence a&b = 0, as a+b = a^b + 2*(a&b)
  28.     // hence a&b = 0, that means a and b have no common bits, if it's off in n then we have a choice of 2
  29.     int n;
  30.     cin >> n;
  31.     int ans = 1;
  32.     while (n)
  33.     {
  34.         if (!(n & 1))
  35.         {
  36.             ans <<= 1;
  37.         }
  38.         n >>= 1;
  39.     }
  40.     cout << ans << endl;
  41.     return;
  42. }
  43.  
  44. signed main()
  45. {
  46.     NeedForSpeed;
  47.     int t = 1;
  48.     // cin >> t;
  49.     while (t--)
  50.     {
  51.         solve();
  52.     }
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment