**The Binary
Number System**

A computer is an **instruction follower.** The types of
instructions that a computer can follow are instructions to **manipulate
symbols** (data). The symbols that a computer can manipulate may represent

- NUMBERS
- TEXT
- PICTURES
- SOUND

But before the computer can process the information, that information
must be stored in the computer's memory. In order
to store it, we must be able to transform it into a form the computer can
understand. The only form in which the computer can represent data is the **digital**
form of **zeros** and **ones.**

So the question
is: How can you represent numbers, characters, pictures, and sound using only
zeros and ones?????

**NUMBERS**

Numbers can be
represented in many different ways. For example, the number 12 can be
represented as:

- ||||| |||||
||
- XII
- 12
- twelve
- 12.00

We could represent
the number 12 in any of the above forms. However, when we use a computer, our
choices are limited. In the early 21st century, we have one choice. The
electronic devices that are used to store data in a computer that are the
cheapest and most reliable are **two-state devices**. An example of a
two-state device is a light bulb. A light bulb can be either on or off (two
states). Furthermore, a light bulb remains in the state in which you place it
until you change it. You could say that it "remembers" what state it
is in. You can turn a light on, leave for a while, and when you return, the
light bulb is in the same state it was in when you left (it remembers).

Computers
contain electronic circuit elements which function the same way. Each circuit
element in the memory of a computer can be placed in either a **high
voltage **state or a **low voltage **state and stay in that
state (it is a memory device!).

One of these
devices by itself can only be in one of two states. It can therefore only
"remember" *yes* or *no*, *on* or *off*, *open
for business* or *closed for business*, **0 or 1**, etc.

But if we group
these two-state devices together, our options increase. Consider three light
bulbs. The possible states the three bulbs can occupy are (x = bulb off, **o**
= bulb on):

x x
x

x x o

x o x

x o o

o x x

o x o

o o x

o o o

There are 8
states, which can be numbered 1 through 8. However, it makes sense that all
bulbs off should represent state 0. Therefore numbering from 0 to 7 might make
more sense. Then a 7 would be represented by all bulbs on. But
what about the other states??? How do we decide which number to assign
to each state???

If we let a bulb
that is ON represent the number 1 and a bulb that is OFF represent
the number 0, then our 8 states are:

`x x x becomes 0 0 0`

`x x `**o**** ** becomes 0 0 1

xo xbecomes 0 1 0

xo obecomes 0 1 1

ox x becomes 1 0 0

o xobecomes 1 0 1

oo xbecomes 1 1 0

oo obecomes 1 1 1

We're used to seeing numbers with the digits 0..9.
In a computer, numbers must be represented using only the digits 0 and 1.
Fortunately, binary numbers (numbers using only 0 and 1) work the same way as
decimal numbers!

Knowing that a number is in **BASE 10** tells somebody two things:

- there are
**10 digits**in the system (0..9) - a place value system based on
**powers of 10**is used.

**EXAMPLE:** 12,345 when interpreted as a base 10 number is 5 x 1, plus 4 x 10, plus 3 x 100, plus 2 x 1000, plus 1 x
10,000. Each digit in the number is interpreted according to its place value,
and the rightmost digit is always the ones column, and
each place as you move left increases by a factor of 10.

Knowing that a number is in **BASE 2** tells somebody the same two
things:

- there are
**2 digits**in the system (0 and 1) - a place value system based on
**powers of 2**is used.

**Example:** 10110101 when interpreted as a base 2 number is
(starting with the rightmost digit):

` 1 x 1 = 1`

plus 0 x 2 = 0

plus 1 x 4 = 4

plus 0 x 8 = 0

plus 1 x 16 = 16

plus 1 x 32 = 32

plus 0 x 64 = 0

plus 1 x 128 = 128

Add all of these up and you get 181.

`The first few numbers in base 2 (with their decimal equivalents in parentheses) are:`

` 1 (1)`

`10 (2)`

`11 (3)`

`100 (4)`

`101 (5)`

`110 (6)`

`111 (7)`

`1000 (8)`

`1001 (9)`

`1010 (10)`

In general, knowing that a number is inBASE ntells somebody the same two things:

- there are
**n****digits**in the system (0 through n-1) - a place value system based on
**powers of n**is used.

Once you understand this, you can do numbers in *any*
base. Two other number bases that are often used with computers are base 8 and
base 16.

**Base 8** has the digits 0 through 7 and the place values
(from right to left) are 1, 8, 64, 512, 4096, etc.

**Base 16** is a little tricky because we have to come up with
16 digits and we're only used to working with 10. The digits in base 16 are 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where A through F represent
the numbers 10 through 15 (in base 10). The place values in base 16 (from right
to left) are 1, 16, 256, 4096, etc.

So, when numbers are stored in a computer, they are stored as **binary
numbers**. If you could look at the memory of a computer, all you
would see are millions of tiny storage devices, each holding either a high or a
low voltage. Each high voltage represents a 1, and each low voltage represents
a 0. Each one of these individual storage cells is called a **bi**nary digi**t**,
or **bit** for short.

The memory of a computer is divided into cells, each holding a specific
number of bits. A standard cell size that is almost universal is 8 bits. Each
memory cell on most computers holds 8 bits. Such a cell is called a **byte**.

**BYTE:** a group of 8 bits. The standard
unit of storage on most computers.

Computer memory is measured in BYTES:

**KILOBYTE**(KB, or K, kilo=1000)**MEGABYTE**(MB, or M, mega=million)**GIGABYTE**(GB, or G, giga=billion)**TERABYTE**(TB, or T, tera=trillion)

**BINARY AND HEXADECIMAL NUMBERS**

Since all numbers are stored in a computer in binary, you
may wonder why we're bothering with other bases like octal and hexadecimal.
Hexadecimal (and octal) numbers may be used as a *shorthand notation for
binary numbers.*** **Binary numbers, while simple,
have two disadvantages:

- since
they only use two digits, it is very easy to make a mistake when copying a
binary number
- since
they only use two digits, even relatively small numbers require a lot of
digits

Hexadecimal overcomes both of these disadvantages. Note that
a 4-bit number can be used to represent the numbers 0 (0000) through 15 (1111).
Note also that each hexadecimal (or "hex" for short) digit can also
represent the numbers 0 (0) through 15 (F). This means that for every 4-digit
binary number, an equivalent hex digit can be used. An 8-bit binary number can
be represented by 2 hex digits, a 16-bit number can be
represented by 4 hex digits, etc. Hex numbers are a fourth as long as their
equivalent binary numbers. They are also easier to copy because 16 digits are
used instead of 2 and you won't get lost as easily in long strings of 0's and
1's. Hex also has the advantage of representing an
8-bit byte with exactly 2 digits (and 8-bit bytes are now almost universal).

**EXAMPLE (Binary to hex):**

Represent the binary number 1100101011010001 in hex.

First divide the number into groups of 4 bits:
1100--1010--1101--0001. Then convert each 4-bit number into its hex equivalent:
1100=C, 1010=A, 1101=D, 0001=1. Therefore the number in hex is CAD1.

**EXAMPLE (Hex to binary):**

Represent the hexadecimal number FACE in binary.

Convert each hex digit into its equivalent binary number:
F=1111, A=1010, C=1100, E=1110. Therefore the number in binary is 1111 1010
1100 1110.

**FRACTIONS**

**BASE 10.**** **Fractions are
easy. If you know how decimal fractions work in base 10, you *already
know *how fractions work in other bases. In base 10, the first place
to the right of the decimal point is the 1/10 place. The next place is the
1/100 place, followed by the 1/1000 place, etc. Every time you move one place
to the right your denominator increases by a factor of 10.

**BASE 2.** The first place to the right of the "decimal"
(actually, it's now a "binary" point, but it's hard to get used to saying
that) is the 1/2 place, followed by the 1/4 place, the 1/8 place, 1/16 place,
etc. Every time you move one place to the right your denominator increases by a
factor of 2.

**BASE 16.** The first place to the right of the "hex" point
is the 1/16 place, followed by the 1/256 place, followed by the 1/4096 place,
etc. Every time you move one place to the right, your denominator increases by
a factor of 16.

**EXAMPLE (converting from base 2 to base 10):**

What is the value in base 10 of the binary number .1011?

Multiply each digit by its place value: (1
x 1/2) + (0 x 1/4) + (1
x 1/8) + (1 x 1/16).

This becomes: 1 x .5 + 0 x .25 + 1 x .125 + 1 x .0625 = .6875

**EXAMPLE (converting from base 16 to base 10):**

What is the value in base 10 of the hexadecimal number .9A2?

Again, multiply each digit by its place value: 9 x 1/16 + A
x 1/256 + 2 x 1/4096.

This becomes: 9 x 0.0625 + 10 x 0.00390625 + 2 x 0.000244140625

Which becomes: 0.5625 + .0390625 + 0.00048828125 =
0.60205078125

**CONVERTING FROM BASE 10 TO BASE 2**

An example is given on page 52 of the textbook, but it's not obvious why
this method works. To see why it works, consider the original binary number:
0.110101. Let's multiply it by 2 repeatedly and drop any whole-number that appears
on the left of the "binary" point. Note that multiplying by 2 in base
2 simply involves moving the binary point one place to the right (just like
multiplying by 10 in base 10 simply involves moving the decimal point one place
to the right):

`0.110101 x 2 = 1.10101`

0.10101 x 2 = 1.0101

`0.0101 x 2 = 0.101`

`0.101 x 2 = 1.01`

`0.01 x 2 = 0.1`

`0.1 x 2 = 1.0`

0.0 Quit

Note that the red numbers, when read from top to bottom, give you the original
digits in the binary fraction. It should be obvious that this is true because
multiplying binary numbers by 2 does not change the digits,
it just changes the position of the binary point.

Now, note that if we start out with the *same fraction*, but
in base 10, and do the *same multiplications* (and again
dropping any whole-number that appears to the left of the decimal point), we
will get the *same digits* to the left of the decimal point!

`0.828125 x 2 = 1.65625`

0.65625 x 2 = 1.3125

`0.3125 x 2 = 0.625`

`0.625 x 2 = 1.25`

`0.25 x 2 = 0.5`

`0.5 x 2 = 1.0`

0.0 Quit

**A USEFUL TOOL**

While it is important to understand how all of this works, it is not
necessary to go through all of the tedious conversion routines by hand. If you
are running Windows, you have access to a tool that does integer number
conversions for you: the Windows calculator desk accessory. Give it a try. Note
that you must be in "Scientific View" to enable number bases other
than base 10. Also note that it does not do fractional conversions.