fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool ispow(long long int n){
  4. if(n==0)
  5. return false;
  6. else
  7. return(floor(log2(n))==ceil(log2(n)));
  8. }
  9. int main() {
  10. // your code goes here
  11. int t;
  12. cin>>t;
  13. while(t--){
  14. long long int l,r,i,m=0,kh;
  15. cin>>l>>r;
  16. kh=r;
  17. if(l==r)
  18. cout<<l<<endl;
  19. else if(ispow(r))
  20. cout<<(2*r-1)<<endl;
  21. else
  22. {
  23. while(r>0)
  24. {
  25. r=r/2;
  26. m++;
  27. }
  28. m--; // m is for max power of 2 that we can represent the number.for e.g 9=2^3+1 i.e max power of 2 is 3.
  29. if(l>(pow(2,m)-1))
  30. cout<<kh<<endl;
  31. else
  32. cout<<(pow(2,(m+1))-1)<<endl;
  33. }
  34. }
  35. return 0;
  36. }
Success #stdin #stdout 0s 4476KB
stdin
4
1 3
4 5
1031 93714
1000000000123 1000000123456
stdout
3
5
131071
1000000123456