In case you are asked to find out the Number which gives minimum and maximum upon XORing with a given number. The concept is simple.
- The minimum XOR result is 0, so the number which produces minimum result upon XOR with a given Number is the Number itself.
- The number which produces the maximum XOR is the given number with all its bits flipped.
Bit Flipping LOgic: 1. Find The number of bit. 2. For each of the bits, transverse once and flip the bits.
The C++ code for flipping is:
int flip(int num) { int x=log2(num)+1; // Finding the number of bits for(int i=0; i<x; i++) num=num^(1<<i); // Flipping Each bits of the given number by XORing with the present bit
return num; }