Bitlength and bound needed for two's complement
Prove that for $A, B \in \mathbb{Z}$, $A + B$ $= (A\operatorname{\&}B) + (A \mid B)$ $= (A \oplus B) + 2(A\operatorname{\&}B)$ where $\operatorname{\&}$ is bitwise AND, $\mid$ is bitwise OR and $\oplus$ is bitwise XOR. (It is reasonable to assume that the bit representation of integers is two's complement.)
You have interpreted item 23 so that it is almost correct.
If we allow $A$ or $B$ to be negative, however, it is hard/impossible to assign/determine the sign of $A\operatorname{\&}B$, $A\mid B$ or $A\oplus B$ if we use the natural/standard representation of a number in base $2$. A different method that can represent signed integers without using the sign $\pm$ has to come into play. You selected two's complement. Then means you should also specify the length of a binary representation as well as restrict the magnitude of $A$ and $B$ to avoid overflow. That kind of specification and restriction is unlikely to be the intention of Rich Schroeppel, the author of this standalone item.
On the other hand, the equalities hold for all integers if integers are represented by two's complement with infinite length, as mentioned by hobbs and the Wikipedia article Two's complement and 2-adic numbers.
Let $A$ and $B$ be nonnegative integers
Here is a simpler way to interpret item 23. The only change is $A$ and $B$ are natural numbers (including $0$) instead.
For all $A, B \in \Bbb N$, $\ A + B$ $= (A\operatorname{\&}B) + (A\mid B)$ $= (A \oplus B) + 2(A\operatorname{\&}B)$ where $\operatorname{\&}$ is bitwise AND, $\mid $ is bitwise OR and $\oplus$ is bitwise XOR.
Let us prove the proposition above.
Suppose the usual binary representation $A$ and $B$ are $\cdots A_2A_1A_0$ and $\cdots B_2B_1B_0$ respectively, where all $A_k$'s and $B_k$'s are either $0$ or $1$. By the definition of bitwise operations as given in the question (or everywhere else),
$$\begin{aligned}
&\qquad(A\operatorname{\&}B) + (A \mid B)\\
&= \left(\cdots + (A_2 \& B_2)2^2+(A_1 \& B_1)2^1+(A_0 \& B_0)\right)\\
&\quad + \left(\cdots + (A_2 | B_2)2^2+ (A_1 | B_1)2^1+(A_0 | B_0)\right)\\
&= \cdots \\
&\quad+ ((A_2 \& B_2)+(A_2 | B_2))2^2\\
&\quad+ ((A_1 \& B_1)+(A_1 | B_1))2^1 \\
&\quad+ ((A_0 \& B_0)+(A_0 | B_0))\\
\end{aligned}$$
We have a similar identity for $(A\oplus B) + 2(A\operatorname{\&}B)$.
Note that $A+B$ also means doing addition place-wise as shown in the following identity.
$$\begin{aligned}
A+B&=\cdots\\
&+(A_2 + B_2)2^2\\
&+(A_1 + B_1)2^1\\
&+(A_0 + B_0)\\
\end{aligned}$$
So it is enough to prove the following identities.
$$\begin{aligned}
&\ \ \vdots\\
A_2+B_2 &=(A_2 \& B_2)+(A_2 | B_2)\\
A_1+B_1 &=(A_1 \& B_1)+(A_1 | B_1)\\
A_0+B_0 &=(A_0 \& B_0)+(A_0 | B_0)\\
\end{aligned}$$
That is, it is enough to show the proposition is true when both $A$ and $B$ are either $0$ or $1$.
There are $4$ cases.
- $A=B=1$. Then
$A+B=2$
$(A\operatorname{\&}B) + (A\mid B) = 1+1=2$
$(A \oplus B) + 2(A\operatorname{\&}B)=0+2\cdot1=2$
- I am sure you will be able to verify the other three cases.
Another interpretation, $A$ and $B$ are integers in programming languages
Consider Java. If A
and B
are of type int
or long
, then (A & B) + (A | B)
and (A ^ B) + 2 * (A ^ B)
are all equal. It does not matter whether A
and/or B
is negative and whether there is overflow. For example, if int A = (1 << 30) + (1 << 29); int B = A - 1;
, then A + B
, (A & B) + (A | B)
and (A ^ B) + 2 * (A ^ B)
are all equal to -1342177280
.
Consider Python. If $A$ and $B$ are of type <class 'int'>
in Python, then those three expressions are also equal. It does not matter whether A
and/or B
is negative.
I would believe the same happens for most programming languages.
It is easy to expect/understand the expressions are equal when none of A
and B
are negative and there is no overflow. To understand why they remain equal without exception otherwise, it may require some digging into the specification/implementation of Java/Python. Or, where is an existing proof?