Skip to content

Instantly share code, notes, and snippets.

@dsc712
Created May 11, 2019 15:20
Show Gist options
  • Save dsc712/32c5fd4ac70e6aec3f69dc2edb6adada to your computer and use it in GitHub Desktop.
Save dsc712/32c5fd4ac70e6aec3f69dc2edb6adada to your computer and use it in GitHub Desktop.
max xor b/w a range
#include <bits/stdc++.h>
using namespace std;
int getMax(int L, int R) {
int LxR = L ^ R;
int p = 0;
// msb of L ^ R
while(LxR) {
p++;
LxR >>= 1;
}
int max = 0;
int two = 1;
while(p--) {
max += two;
two <<= 1;
}
return max;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int tc,L,R;
cin>>tc;
while(tc--) {
cin>>L>>R;
cout<<getMax(L,R)<<"\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment