Why is this true? Does anyone have an explanation for this behavior?
Number parity in binary
In any number in any base, the rightmost digit is always equal to the remainder when dividing a number by its base.
As a simple example, using base 10, pick a random number (no matter how big), divide by 10, and take the remainder. Every time, the rightmost digit of the number you picked is the same as the remainder.
61398643861898841835 % 10 = 5
I didn't need to calculate this. I just looked at the last digit of the number I randomly typed in, and knew conclusively that that was the remainder.
We divided by 10 in the above example because you picked a number in base 10. But since your question is about binary numbers, we're working in base 2, so we'd have to divide the number by 2.
The parity of a number (= it being even or odd) is essentially the same as asking if it can be divided by two without a remainder. Therefore, "does this number have a remainder when divided by two?" is the same as asking "is this number odd?"
The conclusion here is that when written in binary, the rightmost digit of a number is 0 when the number is even, and 1 when the number is odd.
Therefore, we can state that even numbers in binary always follow the pattern ???0
, where ?
represents an unknown value. We know that the last digit is a 0
because the number is even.
The number 1 in binary
Not much needs to be said here. The number one in binary is just 1
, or, using our earlier number format, 0001
.
AND logic
The AND table is fairly straightforward. The result is true only if both inputs are true.
Note that true is the same as 1, and false is the same as 0.
A | B | Output
----------------
0 | 0 | 0
1 | 0 | 0
0 | 1 | 0
1 | 1 | 1
So let's try a thought experiment. Can you tell me the output if I don't tell you what B
is? Since we don't know the value of B, I'll use ?
to represent that unknown value.
Let's examine both options. Assume A is true:
1 AND ? = ...
You cannot actually know the outcome here. Depending on B being true or false, the output will change.
Let's assume A is false:
0 AND ? = ...
Here, you can actually be sure. It's impossible for AND to output true when any of its inputs is false. Since we know that at least one of them (A) is false, we can therefore state that the output is always going to be false, regardless of the value of the other input (B). No matter whether B is true or false, this is not going to change the output.
You can confirm this by looking at the table above. In all cases where A is 0, the output is also 0. There is not a single case where the output is 0
Binary number AND
When you &
two binary numbers, what you're really doing is performing an AND operation between the respective digits.
If you take number ABCD
and EFGH
(where each letter represents a binary bit) and you &
them together, the result will be a four digit number which we'll call IJKL
. The value of these four digits will be:
I = A AND E
J = B AND F
K = C AND G
L = D AND H
Now let's go back to our two numbers.
- The first number is an even number, so we know it's
???0
- The second number is 1, so we know it's
0001
So what is the result of performing &
on these numbers? Well, just like we did with the letters above, the result is going to be a 4 digit number (which I'll call MNOP
), and we know how to calculate each of its digits:
M = ? AND 0
N = ? AND 0
O = ? AND 0
P = 0 AND 1
Remember what we concluded about AND logic: if any of the inputs is false/0, then we conclusively know that the output will also be false/0.
Even though we still have some unknown values in our above calculations, we can already see that every & calculation has at least one 0 in it. Therefore, we can conclude that every calculation's outcome is going to be 0.
This means that number MNOP
will always be 0000
, when one of the inputs was an even number, and the other input was equal to 1.