Binary
Starting with Base 10
Usually with numbers, we write them in “base 10” which means that there are 10 unique digit values:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
If you use a counter with three places, it starts at 000
and increments the
right-most place by one each time: 001
, 002
, 003
, until we reach 009
.
After that, it adds one to the next place and resets the right-most place back
to zero: 010
. Then it continues again 011
, 012
, … 019
, 020
, 021
… 099
, 100
, 101
, … up to 999
.
If you increment one more time, most counters would “wrap” back to
000
- called “overflow”
Other Bases
How would other bases work? If we had a counter that worked differently and moved to the next place sooner or later, then it would be counting in a different base. For example, counting some duration of time would go:
0 hours, 0 minutes, 0 seconds
0 hours, 0 minutes, 1 second
...
0 hours, 0 minutes, 59 seconds
0 hours, 1 minute, 0 seconds
0 hours, 1 minute, 1 second
...
0 hours, 59 minutes, 59 seconds
1 hour, 0 minutes, 0 seconds
1 hour, 0 minute, 1 second
In this example the seconds and minutes have 60 unique “digit” values before the next place is incremented, so it’s almost like it’s “base 60”.
Base 2
Now let’s go the other direction: what if we only had 2 unique digit values?
Let’s choose 0 and 1. Setting up a counter with three places again: 000
, then
start counting: 001
. Wait, looks like we already need to move onto the next
place and reset the right-most digit: 010
. And if we continuing counting:
011
, 100
, 101
, 110
, 111
. Now we’ve gotten as far as we can with three
places - that didn’t take very long. In fact with three places, we can only
increment the counter 7 times! But on the other hand, it’s very easy to count
since each digit is only a 0 or 1. Here is an example counter with 4 places:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Take a look at the patterns that come out! The right-most place flip flops between 0 and 1. In fact, if the digit is 0 we’ve incremented the counter an even number of times. If the digit is 1, we’ve done it an odd number of times. Also notice how the next place flip flops half as frequently, then the next place half as frequently again, then the left-most place only when from a 0 to 1 and not back to a 0.
This base-2 system is called binary and computers that interally use binary
numbers are called “digital” computers. Counting with binary takes a lot more
places than in base-10 which makes it harder for us to write down binary
numbers. Most phones or laptops work with binary numbers that have at least 32
binary places: 0000 0000 0000 0000 0000 0000 0000 0000
(spaces added for
clarity).
When computer processors are designed, one choice that is made is what the number of places they can handle for a number at time, and a computer processor designed for numbers of 32 binary places is called a “32-bit” processor.
Hexadecimal
Since it’s so many places, we also typically work with another base: base-16 also known as “hexadecimal” (hex: 6 + dec: 10). If you have a binary counter with 4 places, you can represent 16 unique values: 0 through 15. This means it’s very easy to convert a number written in binary to a number written in hexadecimal because each group of 4 binary digits becomes 1 hexadecimal digit which means a lot fewer digits. And since 16 is close to 10 it’s also easier for us humans! For example, here is the number “11433” in a counter with 16 binary places:
0010 1100 1010 1001
If we look at each group of four binary digits and treat them as a separate counter with 4 binary places, then these would be the counts they correspond to:
0010 -> "2"
1100 -> "12"
1010 -> "10"
1001 -> "9"
Those numbers greater than 9 are inconvenient because they take two places right
now in decimal, so in hexadecimal the notation that is used include not only the
common 0-9, but also add a 6 more digits: a
, b
, c
, d
, e
, f
. These
represent “10”, “11”, “12”, “13”, “14”, and “15” but can be written with one
letter instead of two. This means now we can write the number with 4 letters in
hexadecimal:
2ca9
Much easier!
Notation
If a number is written as 1010
, usually it’s meant to be read in base-10, but
it could be in base-2, or base-16 so some notation is used to indicate the base:
TK Also subscript to indicate
Common notation for binary is 0b
or b
:
0b1010 or 1010b
Common notation for hexadecimal is 0x
or h
:
0x1010 or 1010h
The reason the prefix
0b
or0x
is because if it wasb1010
orx1010
those could technically be variable names so the prefix starts with0
to signify it is a number
TODO
- Odometer
- Mechanical visualization
- Binary mechanical visualization