D-Or problem from October Cook Off

Here is the link to the problem:

I have been trying to solve this problem from quite a while now. I have understood the logic behind it i.e, to find the first most significant bit that is set in R and not in L and just make all the bits with lower significance than this bit set in the final answer. The previous bits including this found-bit will be the same in the answer that is same in both L and R given to us.
I would really appreciate if someone could let me know where I am going wrong.

Here is my code:

/*Priyansh Agarwal*/
#include<bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define nline "\n" 
typedef long long ll;
typedef unsigned long long ull;
int main()
{
	fastio();
	#ifndef ONLINE_JUDGE
	freopen("Input.txt","r",stdin);
	freopen("Output.txt","w",stdout);
	#endif 
	int t;
	cin>>t;
	while(t--)
	{
		ull a,b;
		cin>>a>>b; // a means L and b means R
		ull ans=0;
		int p=log2(b); //finding the number of bits in the bigger number
		for(int i=p;i>=0;i--)
		{
			if((b & ull(pow(2,i)))>=1)
			{
				ans = ans | ull(pow(2,i)); //making that bit set in the answer which is set in the bigger number
				if((a & ull(pow(2,i)))==0) //checking if this bit is set in L also or not
				{
					ans = ans | ull((pow(2,i)-1));  //making all the bits smaller than this set in the final answer
					break; //breaking as we dont need to check any further
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

log2() rounds off to different values for large integers.
Similar for inbuilt power function. Just implemented my own log and pow functions with your code and it works fine.
D-Or

1 Like

Oh yes how could I miss that๐Ÿ˜…. Thanks a lot bro, really appreciate the reply.

Cheers :v: