|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Counting in Binary: As Easy As 0, 1, 10
|
1 | 10 | 11 | 100 | 101 | 110 | 111 | 1000 | 1001 | 1010 | 1011 | ||||||||||
+ 1 | + 1 | + 1 | + 1 | + 1 | + 1 | + 1 | + 1 | + 1 | + 1 | + 1 | ||||||||||
10 | 11 | 100 | 101 | 110 | 111 | 1000 | 1001 | 1010 | 1011 | 1100 |
Another way to count in binary is the exact same way you count in decimal everyday. Step One, start with a single digit and run through each symbol one after the other: like 0,1,2,3,4,5,6,7,8,9. Step Two, when there are no more symbols to run through, just do this: add another digit, increase the symbol on the left by one, and then repeat step one: like 10,11,12,13,14,15,16,17,18,19,20,21,… You get the idea.
Now let's apply the same process to binary numerals: Step One, start with a single bit and run through each symbol one after the other: like 0,1. Step Two, when there are no more symbols to run through, just do this: add another bit, increase the symbol on the left by one, and then repeat step one:
Decimal Numerals | Binary Numerals |
---|---|
0 | 0 |
1 | 1 |
2 | 10 |
3 | 11 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
Now I am going to need your help retrieving some stuff from your implicit memory so we can compare binary numerals to decimal numerals. At this point in your life you probably take it for granted that you understand the difference between 2976 and 6792. But if you think back to grade school for a moment you may remember having to learn the difference between units, tens, hundreds, thousands, ten thousands, and so on. And then your teacher probably made you do things like this: 6792 = (6 × 10³) + (7 × 10²) + (9 × 10¹) + (2 × 10º) = (6 × 1000) + (7 × 100) + (9 × 10) + (2 × 1) = 6000 + 700 + 90 + 2 = 6792. Ah… the good old days… Okay now, let's come back to reality. The reason you had to learn this process was so you would understand that the decimal system is weighted by position. Weighted by position means because the decimal system has ten different symbols (0 to 9), each position is a multiple of ten of the rightmost position (A fancy phrase experts and showoffs like to use is positional notation with a radix of ten).
The binary system is a positional system with a radix of two! (Yes, I am an expert.) And what I mean is that each position is a multiple of two of the rightmost position. For examples, let's convert the number six from binary to decimal.
BINARY: | 1 | 1 | 0 |
---|---|---|---|
1 × 2² | 1 × 2¹ | 0 × 2º | |
1 × 4 | 1 × 2 | 0 × 1 | |
4 | 2 | 0 | |
4 + 2 + 0 | |||
DECIMAL: | 6 |
Now that you have learned how to count in binary, let me show you two other operations that you will likely have to perform frequently: conversion of decimal to binary and binary subtraction.
Converting decimal to binary is as easy as dividing by two. Just take the decimal number you wish to convert to binary and divide it by two. Record the remainder. Repeat the process with the result until the result is zero. Let's convert 12 to binary for a demonstration:
Number/Divisor | = | Result | Remainder |
---|---|---|---|
12/2 | = | 6 | 0 |
6/2 | = | 3 | 0 |
3/2 | = | 1 | 1 |
1/2 | = | 0 | 1 |
In the decimal system, we perform subtraction by adding to a negative number; like this: 9 + (- 4) = 5. Since a computer stores everything in 0s and 1s — hence no room for a negative sign — we must devise a system to distinguish between positive and negative values. Many different systems have been implemented to compute negative binary numbers. The one we will discuss here is called Two's Complement. In the two's complement system, we simply take a binary number, flip every bit (change all 1s to 0s and all 0s to 1s), then add one to the new numeral. Let's demonstrate: 7 = 0111. Hence, to get -7, first we flip every bit to get 1000; then, we add 1 to get 1001. Therefore, -7 = 1001. Notice how I wrote the 7 with a leading zero, 0111. Here is why: a computer that uses the two's complement system starts all positive numbers with a 0 and all negative numbers with a 1; otherwise there would be no way to distinguish between positive and negative numbers.
Now that we know how to represent negative numbers, let's talk about subtracting numbers. You see, computers save information in memory of specific size: 2-bit, 4-bit, 8-bit, 16-bit, 32-bit, etc. Say we have a computer with a 4-bit memory chip. Then to represent the number 2 we must write 0010. So what would -2 look like? Try to do it yourself and see if our answers match:
Step one: Flip every bit in 2 to get 1101.Now if indeed 1110 represents -2, we should be able to add 2 to it to get zero. So let's check 2 + (-2) = 0:
0010 |
+ 1110 |
10000 |