Maximum and Minimum XOR of a Given Number

Revision en1, by Blaize11, 2020-10-25 09:14:09

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.

  1. The minimum XOR result is 0, so the number which produces minimum result upon XOR with a given Number is the Number itself.
  2. 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; }

Tags #xor, #flip, #bitflip, #minmaxxor, #min, #max, #maxxor, #minxor

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Blaize11 2020-10-25 09:14:09 774 Initial revision (published)