// Program by Akash Tripathi (@proakash256)
// To find the Maximum value of XOR
// between two numbers.
// If we take the XOR of any two
// numbers for Max. value, their first
// bit will be fixed, which will be same
// as the first bit of XOR of L and R
// itself. The Most Significant Bit of this
// XOR tells us the Maximum Value.
// Let the XOR of L and R is 1xxx
// where x can be 0 or 1 then the
// max value we can get is 1111.
#include <stdio.h>
int main()
{
int L , R;
printf("Enter two Numbers :\n");
scanf("%d" , &L);
scanf("%d" , &R);
int xor = L ^ R;
int msbPos = 0;
while (xor > 0)
{
msbPos = msbPos + 1;
xor = xor >> 1;
}
int max = 0;
int two = 1;
while(msbPos > 0)
{
max = max + two;
two = two << 1;
msbPos = msbPos - 1;
}
printf("The Maximum value of XOR
b/w %d and %d is %d." , L , R , max);
return 0;
}
Comments
Post a Comment