In low level programming, it is important to understand binary numbers. This is because almost all digital computers as of now are binary. This means that everything is represented in binary, and all operations are carried out in binary.
The reason why most computers are binary is that they are made out of binary gates. A binary gate accepts binary inputs and produces binary outputs. In electrical terms, a gate recognizes a low voltage as one state (usually 0) and a voltage higher than a threshold as another state (usually 1). Similarly, the output also has two states, one representing 0, and one representing 1.
Binary numbers are actually easy to understand, more so than decimal numbers! To recap decimal numbers, the number 1742 means one thousand seven hundred forty two. Let’s see why that is the case:
Pretty easy stuff. Now, let us take a look at a binary number, say . The suffix 2 means this is a binary number, as opposed to a decimal number.
You see, each digit of a binary number represents the quantity of of a specific power of 2, just like each digit of a decimal number represents the quantity of a specific power of 10. The trick to convert a binary number to a decimal number relies on the fact that is true in any base, and that is 2.
Decimal addition requires at least one year of learning. That is because there are 100 possible cases of , where and are single decimal digits. Even with the commutative law and 0 as an identity, there are still 36 unique pairs.
Binary addition, however, only has 4 combinations.
is easy to understand. 0 is 0 in any base.
and are also easy to understand because 0 is the identity of addition.
is a little new, sort of. The quantity of the addition cannot be represented by a single digit in base 2. That’s why it has two digits. Another way to look at this is that “one plus one is zero with a carry of one”. This is not unlike in base 10, “six plus seven is 3 with a carry of one”.
Generally speaking, we can perform multidigit binary addition like we perform multidigit decimal addition. The key to do this by hand is to introduce a row just for the carry from a previous bit position.
We’ll use one example here, lets add to :
When we begin, we have
The first digit (bit 0) has a result of zero, but the addition also yields a carry for bit 1:
The sum of bit 1 from the numbers is 0 with a carry, then this zero is added to the carry from bit 0, resulting in a 1 as the final result for bit 1:
As for bit 2, the sum of the two numbers is a 1, with no carry to bit 3. However, when we add this partial sum to the carry from bit 1, the final result is a 0 with a carry to bit 3:
This gets boring after a while, the final solution is as follows:
Once we understand addition, subtraction is very similar.
is easy.
is also easy.
is obvious.
What is ? One way to look at this is , which is simple. However, another way to look at this is with a borrow from the next bit.
This module makes it rather clear that not only can we represent numbers in binary and perform addition and subtraction in binary, but it is actually easier!
It’d be easier if we were never taught decimal numbers!