Help to code, i am not getting which test case is failing

#include<bits/stdc++.h>
using namespace std;

int main () {

long long x,y;
cin >> x >> y;
long long ans = 0;
bool first = false;
for(int i = 18; i >= 0 && y != x; i--){
	//(1 & 5>>2)
	int x_bit = ((x>>i)&1);
	int y_bit = ((y>>i)&1);
	if(first == false && y_bit == 1){
		first = true;
		if(x_bit == 0){
			ans += (1 << i);
		}
		continue;
	}
	if(first){
		ans += (1 << i);
	}
}
cout << ans << endl;
return 0;

}

what is wrong in this logic, i am not getting which test case is failing

hi @mehul.narendra.dubey here is the code that uses the hint given in the video. I am not sure what exactly is the logic you are using, so please explain it a bit if you need help with that. Also, please save your code on ide.codingblocks.com and share the link, for future reference.

suppose i have two numbers 13 and 7 , these are my boundaries
13 --> 1 1 0 1 , 7 --> 0 1 1 1 , bit number 4 will make xor 1, so 1<<3 will be in my ans, and after that all the bits i can make it one, that is the reason after that i am adding all the powers of 2

@mehul.narendra.dubey you are thinking in the right direction, I am using the same logic. (finding the left most bit that is not the same, and making all the bits after that 1 so that the answer looks something like 00…1111) remember that you make the bit that is uncommon 1 as well. For example for 5 and 6
101
110 the first differing bit from the left is the second bit from the right
so our answer will become 0000…11, ie 3

SO to summarise, find the left most differing bit, and make all the bits, including that bit 1, and all the bits to the left of that bit 0.

thank you @Ishitagambhir

1 Like

@mehul.narendra.dubey please mark the doubt as resolved