~(~x + 1) + 1 =
(definition of subtraction: a - b = ~(~a + b))
x - 1 + 1 =
(you know this step)
x
另外, ~x + 1 == ~(x - 1) . 为什么?好,
~(x - 1) =
(definition of subtraction: a - b = ~(~a + b))
~(~(~x + 1)) =
(remove double negation)
~x + 1
那个(略微不寻常)的减法定义, a - b = ~(~a + b) ?
~(~a + b) =
(use definition of two's complement, ~x = -x - 1)
-(~a + b) - 1 =
(move the 1)
-(~a + b + 1) =
(use definition of two's complement, ~x = -x - 1)
-(-a + b) =
(you know this step)
a - b
2 回答
无论如何,这是一回事 . 那就是
~x + 1 == ~(x - 1)
. 但是现在让我们把它放在一边 .f(x) = ~x + 1
是它自己的逆 . 证明:另外,
~x + 1 == ~(x - 1)
. 为什么?好,那个(略微不寻常)的减法定义,
a - b = ~(~a + b)
?这是因为如果你增加~x(假设没有溢出) . 然后将其转换回x,相对于~x递增,但相对于x递减 . 同样的事情也适用 . 假设你的变量x有一个特定的值,每次递增它时,相对于~x你会注意到它递减了 .
从程序员的角度来看,这是你基本上见证的 .