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

Автор HotashCoder, история, 18 месяцев назад, По-английски

a+b=x, a|b=y, a&b=z, a^b=k

If x,y,z,k is given, is it possible to find a-b?

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

»
18 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

If we know, that a|b=y and a&b=z, then we can uniquely determine x and k, using this equations (you can read about it here)

a+b = a|b + a&b

a+b = a⊕b + 2(a&b)

In case we know y and z, determine:

x=y+z

k=x-2z

So from this point, we can use x and k only to check, if there exists a and b, that satisfy bitwise properties.

Let's assume that we find such a and b, that a|b=y and a&b=z, then notice, if we swap i-th bit of a and b, then x, y, z, k will not change, but a-b can

Example:

a=1100

b=1011

a-b=1

let's swap 3-rd bit:

a=1000

b=1111

a-b=-7