Jump to content

Wikijunior:How Things Work/Binary Numbers

From Wikibooks, open books for an open world
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What is Binary?

[edit | edit source]

Most people use ten different digits — 0 to 9 — to write numbers. For example, the numbers 15, 987630, 100, 99, and 6 are all made up of one or more of these ten digits.

This is called the decimal number system or base ten, which means that this number system has ten different digits to construct a number, as many as (most) people have fingers.

But computers are not built with the decimal number system. This is because computers are built with electronic circuits, each part of which can be either on or off. As there are only two options, they can only represent two different digits, 0 and 1. This is called the binary number system, or base two. ("Bi" means two.) All the numbers are constructed from the two digits 0 and 1. A digit in binary (that's a 0 or a 1) is also called a bit – short for binary digit.

Computers use this number system to add, subtract, multiply, divide and do all their other math and data. They even save data in the form of bits.

A bit by itself can only mean zero or one, so to represent bigger numbers (and even represent letters) they group them together into chunks. Eight bits make a byte, and computers use as many bytes as they need to store what we need them to. Modern computers have many billions of bytes of storage.

This book will teach you how binary works, why computers use it, and how they use it.

Why do we use Binary?

[edit | edit source]

In normal math, we don't use binary. We were taught to use our normal number system. Binary is much easier to do math in than normal numbers because you only are using two number-symbols — 1 and 0 instead of ten number-symbols — 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Computers use binary because they can only read and store an on or off charge. So, using 0 as "off" and 1 as "on," we can use numbers in electrical wiring. Think of it as this — if you had one color for every math symbol (0 to 9), you'd have ten colors. That's a lot of colors to memorize, but you have done it anyway. If you were limited to only black and white, you'd only have two colors. It would be so much easier to memorize, but you would need to make a new way of writing down numbers. Binary is just that — a new way to record and use numbers.

Binary Notation

[edit | edit source]

In school, you were taught that we have ones, tens, and hundreds columns and so on. Each column is worth ten times the amount of the column to its right. This means that if the first column is worth 1, the second column is worth 10, and the third column is worth 100, and so on. To find the value of a digit in a certain column, you multiply the digit by the number that the column is worth. If you haven't seen a model of the columns before, it looks like this:

Decimal column 10,000 1000 100 10 1
Digit 5 4 9 3 6
Value 5 × 10,000 4 × 1000 9 × 100 3 × 10 6 × 1

So the decimal number 54,936 is equal to 5×10000 + 4×1000 + 9×100 + 3×10 + 6×1.

Binary also has columns, but each column is worth two times the amount of the column to its right. You still determine the value of digits in the same way, by multiplying the value of the bit (0 or 1) by the value of its column digit (128, 64, 32, etc.), as shown below:

Binary column 128 64 32 16 8 4 2 1
Bit 1 0 1 1 0 1 0 1
Value 1 × 128 0 × 64 1 × 32 1 × 16 0 × 8 1 × 4 0 × 2 1 × 1

So the binary number 10110101 = (1×128) + (1×32) + (1×16) + (1×4) + (1×1) = 181 in decimal.

Writing Binary Numbers

[edit | edit source]

The method above lets us read binary numbers, but how do we write them? One way is to write a list of all the numbers starting from one and working upwards. Just as adding 1 to 9 in decimal carries over to make 10, and 1 + 99 makes 100, in binary when you add one to one, you carry a one over to the next place on the left. Follow along with this table to see how that works.

Base-10 Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000

You'll notice that the values 1, 2, 4, 8, and 16 in binary only have a single one bit and some zero bits. If you go back up to the binary columns, you might notice that there is a column for each of these values. If you write the number in the columns, you would only need to write a 1 in the column that has the same value as the number you are writing. All of the other columns would have zeroes. Base ten works in the same way! When you write a base ten number that has the same value of one of the base ten columns, such as 100, you would only write a 1 in the column with a value equal to the number you're writing. You would put a 0 in the other columns.

Base-10 version Binary
1 1
2 10
4 100
8 1000
16 10000

Have you noticed a pattern in writing binary numbers? Study the table for 1 to 16 again until you understand why in binary,

"1 + 1 = 10" and "1 + 100 = 101"

in your own way.

You've probably got lots of practice reading decimal but none yet reading binary, so it's normal for reading binary to feel quite slow.

Writing Larger Numbers
[edit | edit source]

What do you do if you want to write a bigger number, like 86? You could use the list method shown above, but that would take a long time! A quicker method involves the columns:

  1. Find the column with the largest value that is still less than or equal to the number you're writing and write a 1 in that column. For 86, you would write a 1 in the sixty-fours column because 64 is less than 86, but the next column to the left (128) is greater than 86.
  2. Look at the next column to the right of the one you used in the previous step. If you can add the value of that column to the value of the previous column and get a number less than or equal to the number you're converting to binary, write a 1 there. If not, write a 0. In the example with 86, the value of the first column you used (64) plus the value of the next column to the right (32) equals 96. 96 is greater than 86, so you would write a 0 in the thirty-twos column.
  3. Find the next column to the right of the last one you wrote in. If adding the value of that column to the sum of the values of all the columns with a one in them would give you a number less than or equal to your number, write a 1 in that column. If not, write a 0. With 86, the next column would be the sixteens column. 16 plus the sum of the values of all the previous columns with ones in them (64 in this example) is 80. 80 is less than 86, so you would write a 1 in the sixteens column.
  4. Repeat step 3 until the sum of the values of the columns with ones in them is equal to the number you were trying to find. Once this happens, fill any remaining columns with zeros.

A table showing the full process with 86 can be seen below:

Binary column 128 64 32 16 8 4 2 1
Can this number fit? No. The total would be 128, and 128>86. Yes. The total would be 64, and 64<86. No. The total would be 96, and 96>86. Yes. The total would be 80, and 80<86. No. The total would be 88 and 88>86 Yes. The total would be 84, and 84<86. Yes. The total would be 86. No. The total would be 87, and 87>86.
Bit A 0 can be put here, or it can be left blank. 1 0 1 0 1 1 0
Total 0 64 64 80 80 84 86 86

Translating to Base-10

[edit | edit source]

The binary number for 52 is 110100. How do you read a binary number?

  1. You look at the ones column. Since it has a 0 in it, you don't add anything to the total.
  2. Then you look at the twos column. Nothing, so we move on to the next column.
  3. We have a 1 in the fours column, so we add 4 to the total (total is 4).
  4. Skipping the eights column since it has a 0, we have come to a 1 in the sixteens column. We add 16 to the total (total is 20).
  5. Last, we have a 1 in the thirty-twos column. We add this to our total (total is 52).

We're done! We now have the number 52 as our total. The basics of reading a base-2 number is add each columns value to the total if there is a 1 in it. You don't have to multiply like you do in base-10 to get the total (like the 5 in the tens column from the above base-10 example) because the only digits are 0 (Remember that 0 times anything equals 0.) and 1 (Remember that 1 times anything is always equal to the other number.) Not needing to multiply can help speed up your reading of base-2 numbers. Let's look at 110100 in a table.

Binary digit Column Binary digit's value
0 1 0
0 2 0
1 4 4
0 8 0
1 16 16
1 32 32
Total 52

Now let's look at another number.

Finding a Mystery Number

[edit | edit source]

The binary number is 1011, but we don't know what it is. Let's go through the column-reading process to find out what the number is.

  1. The ones column has a 1 in it, so we add 1 x 1 to the total (total is 1).
  2. The twos column has a 1 in it, so we add 1 x 2 to the total (total is 3).
  3. The fours column has a 0 in it, so we add 0 x 4 to the total (total is still 3).
  4. The eights column has a 1 in it, so we add 1 x 8 to the total (total is 11).

We are done, so the total is the answer. The answer is 11! Here are some more numbers for you to work out.

1

101=

2

1111=

3

10001=

4

10100=

5

101000=

Storing text

[edit | edit source]

Computers store everything in binary, including text. To do this, every letter, every punctuation character, in fact a very large number of the symbols people have ever used, has been given its own number in a system called Unicode.

For example, if your name is "George" then the computer can store that in binary just by storing the number for "G", then for "e", and so on. The most common symbols in American English, like letters without accents, can be stored with just one byte. Other symbols, like "£" and "¿", need more than one byte as they've been given a bigger number. A few examples:

  • G is stored as 71, which is "0100 0111" in binary
  • e is stored as a hundred and one, which is "0110 0101" in binary.

The whole word "George" looks like:

0100 0111 0110 0101 0110 1111 0111 0010 0110 0111 0110 0101

While this might look like gibberish, see if you can find the rest of the letters in that and what their decimal representation is!