I try it by using two ways.
check it:-
I try it by using two ways.
check it:-
@sonu28sharma99,
In the first code, ans1 and ans2 are always 0, so you are basically printing a^b for any input a and b, which is obviously wrong, (Why, for e.g x=4, y=12, your code gives 8, but I can have 15 by taking 7 and 8)
And second code is same as first, because a^a is always =0
I’m not understanding what question say’s, please give me some logic what i can do.
@sonu28sharma99
In this question,we are having two number as a input and in between the range of these number we have to calculate the xor and we have to store the maximum value of xor in a variable and at last we have to print the maximum value of xor.
For reference you can see this code:https://ide.codingblocks.com/s/212136
@sonu28sharma99 @Nisha21,
There is a better O(log(max(a,b)) approach, rather than a just iterating from a to b.
Try to solve this, given a,b <= 10^9.
@Nisha21,
Problem <here>
My Code <here>
Take x^y, extract its MSB position, ans = 2^(MSB_position)-1.
Suppose x (in base 2) = 1 1 1 1 1 1 0 0 1…0 1 0 1
Suppose y (in base 2) = 1 1 1 1 0 0 1 1 0…0 0 1 1
Now , x ^ y (in base 2) = 0 0 0 0 1 1 1 1 1…0 1 1 0
Now basically, the MSB of x^y gives us the first leftmost unequal bit in x and y. And we can see we can always find two numbers b/w x and y whose xor = 0000111111111…111, where first 1 is at the MSB position found in x^y.
For instance, take example of x=2 and y=9,
x = 0010
y = 1001
x^y=1011
Max value xor pair = 1111 (take a=7(0111) and b=8(1000))