Binary and Binary Code
Everything you need to know to sound impressive.
Everything you need to know to sound impressive.
Notice:: This thread is a work in progress. I will post sections as they are completed.
Table of Contents:
I. A Brief Description of the Binary System
II. Math in Binary
- Addition
Subtraction
Multiplication
Division
- Decimal to Binary
Binary to Decimal
Hint: Ctrl+F is handy when it comes to finding things in long stretches of text. wink
--
I. A Brief Description of the Binary System
Binary is just another way of counting, similar to the standard decimal system we have been brought up with. Our number system (the decimal system) functions in base 10, meaning that there are ten different numerals that may appear in any given number: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 (count 'em, it's ten). Binary code functions in base 2, meaning that there are only two numerals that may appear in any given number: 0 and 1. "Binary Code" is the use of the binary system in 8-bit communication; this will be examined later on.
--
II. Math in Binary
Addition:
Rules to remember:
1 + 1 = 10
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0
Addition using binary works just the same as it does in decimal. The only difference is that when you add two 1's together, you get 10. In this case, you would write down the 0 and carry the 1 to the next column to be added there.
Let's start with some simple numbers: 101 + 11. We set up the problem just like we would any other addition set:
..101
+ 11
-----
First we add the numbers in the ones column, 1 + 1. This, as mentioned in the rules to remember, produces 10. We write the 0 in the ones place of the sum, then carry the 1 to the next column.
...1 (carried numbers)
..101
+ 11
-----
....0
..101
+ 11
-----
....0
Next, we add the numbers in the twos column (tens column in decimal). Here, we have 1 + 0 + 1. Going from left to right, 1 + 0 = 1, and 1 + 1 = 10. Therefore we write the 0 in the tens place of the sum and carry the 1 to the next column.
..11 (carried numbers)
..101
+ 11
-----
...00
..101
+ 11
-----
...00
Finally, we add the numbers in the fours column (hundreds column in decimal). Although there is seemingly no number in the hundreds place of "11" to add to the 1 in "101", there is, just as in decimal addition, an implied 0. Now, we have 1 + 1 + 0. From left to right, 1 + 1 = 10, and 10 + 0 = 10 (any number plus 0 is the number itself). We place the 0 in the fours (hundreds) place and the 1 in the eights (thousands) place. Therefore we have:
.111 (carried numbers)
..101
+ 011
-----
.1000
..101
+ 011
-----
.1000
If you're wondering, these numbers, in decimal, represent 5 (101), 3 (11), and 8 (1000).
5 + 3 = 8.
--
Subtraction:
Rules to remember:
10 - 1 = 1
0 - 1 = 1 (same as above: borrow a 1 from the next column)
1 - 1 = 0
1 - 0 = 1
0 - 0 = 0
Subtraction, much like addition, also follows the same principles as decimal subtraction. The key thing to remember is that a 1 in one column represents two 1's in the column to the right of it. In this way, when you need to borrow a number, the 1 in the left column becomes a 0 and two 1's are added to the right column.
Let's begin with the numbers 101010 and 1101.
.101010
- 1101
-------
First, in the ones column, we have 0 - 1. Since 1 cannot be subtracted from 0 without a negative result, we must borrow from the next column. The 1 in the twos (tens) column now becomes a 0, and two 1s are written in above the 0 in the ones column in place of the 0. Now we have 1 + 1 - 1, which is 1.
......1
......1
.101000
- 1101
-------
......1
......1
.101000
- 1101
-------
......1
In the twos (tens) column, we have 0 - 0, which is simply 0. In the fours (hundreds) column, we have yet another subtraction which requires borrowing from the next column. The same process is taken.
....1 1
....1 1
.100000
- 1101
-------
....101
....1 1
.100000
- 1101
-------
....101
Again, in the eights (thousands) column, we find borrowing to be necessary. However, there is no 1 in the next column, so we must look even farther and borrow from the thirty-twos (hundred thousands) column. Just as before, the 1 becomes a 0 and two 1s are written in above the 0 in the sixteens (ten thousands) column. Now we have something to borrow from, so ONE of these 1s becomes a 0, and two 1s are written in above the 0 in the eights column, which can now be subtracted from.
..011 1
..111 1
.000000
- 1101
-------
..11101
..111 1
.000000
- 1101
-------
..11101
Thus, we get 101010 - 1101 = 11101. In decimal, this would represent 42 - 13 = 29.
--
Multiplication:
Rules to remember:
* = multiplication sign
1 * 1 = 1
0 * 1 = 0
1 * 0 = 0
0 * 0 = 0
Multiplication with binary is almost exactly the same as regular multiplication. Anyone who knows how to perform regular multiplication with decimal numbers (no lattice technique) should be able to do this easily. We will also be using addition as part of multiplication, so if you haven't learned the addition yet, do it now!
We'll start with two four-digit numbers: 1010 and 1100. These numbers are set up just like a regular multiplication set:
..1010
x 1100
------
Next we multiply each digit in 1010 by the digit in the ones place in 1100, which is 0. 0 times anything is still 0, so in the first row of the product, we write four 0's.
..1010
x 1100
------
..0000
Moving on to the twos place, we would write down a 0 in the ones place of the next line of the product, representing the held ones place. Then we multiply the digit in the twos place in 1100, which is again 0, so it ends up the same as last time.
..1010
x 1100
------
..0000
.00000
Now the fours place of 1100, which is 1, is multiplied, starting on the right in the ones place of 1010 and working to the left. 0 x 1 = 0, 1 x 1 = 1, 0 x 1 = 0, and 1 x 1 = 1. We also have to insert two 0's after this to represent the ones and twos places in the product since this is the fours place we're multiplying, so the next product line would read as 101000.
...1010
x 1100
-------
...0000
..00000
.101000
We've now reached the eights place of 1100, which turns out just like the previous operation did since it is also a 1. Instead of inserting two 0's at the end, though, we insert three (one for the ones place, one for the twos place, one for the fours place).
...1010
x 1100
-------
...0000
..00000
.101000
1010000
-------
Now all that's left is to add the product lines together. Use those addition skills!
.....1010
x .. 1100
---------
.....0000
....00000
...101000
+ 1010000
---------
..1111000
So we have finally reached our answer: 1010 x 1100 = 1111000. In decimal, this would be 9 x 12 = 108.
--
