Xor profit problem

code :
#include
using namespace std;

int main () {
int x, y;

cin>>x>>y;

int xo = x^y;
int setbit = 0;
while(xo != 0){
	xo /= 2;
	setbit++;
}
setbit;
int newbit = 1 <<(setbit);
//cout<<newbit<<" "<<setbit<<endl;
int ans = newbit - 1;
cout<<ans;
return 0;

}

although I passed all the test cases but I am not very much satisfied with explanation of geeks for geeks please tell me the logic of the 1XXX form that is find rightmost set bit of upper and lower no. and rest all bits right to it will be 1, please explain this to me once

Hi Divya
Lets take an example. We have two numbers a and b , a<=b
we take c = a XOR b.
Suppose no. of bits of c = 5. Now looking bits of c from left side, we find that 1st and 2nd bit are 0 and 3rd bit =1.
What do we get to know about it?
that the 1st and 2nd bit are same in a and b. 3rd bit in a = 0 and b=1. So all the no. between a and b have 1st and 2nd bit same as a and b, they only vary from 3rd bit, means that in the XOR of any two no. between a and b, 1st and 2nd bit will be 0. So it will be like 00xxx. Maximum value of it can be obtained by putting all x to 1.

It’s a bit tricky to understand.
Hope it helps.
Give a like if you’re satisfied :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.