Range AND cpp

where i am getting wrong?
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc;
cin>>tc;
while(tc–)
{
long long a,b;
cin>>a>>b;
long long x=floor(log2(a));
long long y=floor(log2(b));
long long ans=0;
if(x==y)
{
ans=a+(a&(a+1))*(b-a);

    }
    else
    {
        ans=(a&(a+1))*(pow(2,x+1)-a-1)+a;
       
    }
  
    cout<<ans%1000000007<<"\n";
}
    

return 0;

}

I have not gone through your logic but
it seems

ans=a+(a&(a+1))*(b-a);

and

ans=(a&(a+1))*(pow(2,x+1)-a-1)+a;

are causing errors ,

overflow is occurring in these lines,
apply mod in these lines and replace the pow function with modular exponention variant , and it may give AC if the rest of the logic is correct

1 Like