Блог пользователя Blaize11

Автор Blaize11, история, 4 года назад, По-английски

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; }

Полный текст и комментарии »

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится