Mixing addition and subtraction with logical NOT
I found some exercises where you combine n-bit 2's complement values in different ways and simplify the output where possible. (Their practice exercises use 16-bit, but that's irrelevant).
Eg:
!(!x&!y) == x|y
0 & y, negate the output == -1
I'm having no problem applying De Morgan's laws with the examples开发者_如何学C using AND, OR, and NOT but I am having difficulty using NOT with + and -
Eg:
!(!x+y) == x-y
!(y-1) == -y
How does NOT distribute?
Edit: responding to comments: I realize this is a bitwise NOT. My question is: in algebraic terms, how does it distribute as per algebra? Example on Wikipedia
With 2's complement numbers when you bitwise NOT them it is the same as saying the negative of the number minus 1, so !x
is equivalent to -x - 1
where x can be a single variable or an expression.
Starting with !(!x+y)
, well !x
is going to be -x - 1
so then it is !(-x - 1 + y)
which becomes -(-x - 1 + y) - 1
which simplifies to x - y
.
And for !(y-1)
, that becomes -(y - 1) - 1 = -y + 1 - 1 = -y
.
精彩评论