Binary

Edited 28th February 2005

Introduction

What is binary?
Computers run on electricity, when data is sent around a computer its sent as patterns of pulses of electricity.
Binary is a way to represent those patterns.

How does it work?
Hopefully you remember the units, tens, hundreds, thousands idea from back in school, that idea is used throughout this topic!

In this idea we count in tens.
We have ten numerical characters that we can use (0,1,2,3,4,5,6,7,8,9)
Once we get to the last character we begin a new column.

However, computers use electricity!
With electricity there can either be a pulse of electricity or no pulse of electricity, there either is one or there isnt, there cannot be half a pulse!

This provides a computer with two numerical characters - a pulse or no pulse.
A '1' represents a pulse, and a '0' represents no pulse.

So, with binary you count in twos!

top

Number systems

We use a system counting in tens, and a computer in twos.

Theres no need for us to count in tens, its just handy because we have ten fingers and thats what were taught to do.
The ancient babylonians used to count in 60's !!! it required a lot of symbols and im sure it was difficult to count in.
We actually still use 60's today - 60 seconds=1min, 60min=1hour, 6x60=360degrees(1 revolution).

In the world of computing we also have two more systems to use - hexidecimal, and octal.
A computer can only count in twos (binary), but it displays the binary in a form we are used to and can understand (10's), but sometimes it can be more useful to us to have it displayed in a different format - in hex(16's) or octal(8's).

Note:
We count in tens (denary!) - we call this base10
Binary is in twos - so this is called base2
Likewise, hex is base16, and octal is base8

top

Decimal/Denary/Tens system in more detail

We need to look at the units, tens, hundreds system in a little more detail, we start of with this:

thousands | hundreds | tens | ones

Now, in more detail:

103          | 102         | 101           | 100
thousands | hundreds | tens          | ones

It might seem confusing, but really its quite simple.
In the number 22, you know that the two symbols are the same but have different meanings because of there positions in the number.
The first 2 is actually 2x10, and the second is 2x1.
And thats what the extra bit represents.

The right most column is x100 (anything to the power of 0 is 1 !) and so is x1.
The second column from the right is x101 and so is x10.
The third from the right is x102 and so is x100.
The fourth from the right is x103 and so is x1000.
And on it goes

This means that the base10 (10's) number 1383 actually represents:
(1x1000) + (3x100) + (8x10) + (3x1)

Adding the columns up you get a base10 total of 1383.
It might seem pointless doing what i just did but you need to be able to understand the principle to understand and use binary and other formats.

You should have noticed that:
1)The power goes up as it goes left, this keeps going, on and on and on!
2)We always used 10 times a power.

top

The Binary System

Well that was base10 which uses 10's, we are now moving on to binary which uses 2's.

Note:
Each pulse (1) or no pulse (0) is called a 'bit'.
A set of bits is called a 'byte'.

In binary, the columns are x2 to a power.

So one byte, using eight bits (columns):

27                                           | 26            | 25             | 24          | 23          | 22           | 21          | 20
one-hundred-and-twenty-eights | sixty-fours | thirty-twos | sixteens  | eights     | fours       | twos       | ones

So, here we have
20 = 1 = 1's column
21 = 2 = 2's column
22 = 4 = ...
23 = 8 = ...
24 = 16 = ...
25 = 32 = ...
26 = 64 = 64's column
27 = 128 = ...

Now you understand how the columns are worked out you can forget that extra bit we put above the columns.

We now just use this table:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
      |      |     |      |   |    |    |

We only have two characters we can fill any column with, a '0' or '1'.

The technique to fill in a binary number is to start at the left!

If you wanted to write the number 22 in binary:
You have no 128's, no 64's, and no 32's, but you do have a 16, so put 0's in 128,64,and 32, and a 1 in 16.
You now have 6 left - you dont have an 8 so put a 0, you do have a 4 so put a 1.
Now you have 2 - you do have a 2 so put a 1.
Now you have 0 - you dont have a 1 so put 0.

You now end up with this:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
  0  |  0  |  0  |  1  | 0 | 1 | 1 | 0

Removing the columns: 00010110

So 22 in base10 is 00010110 in base2!

To convert the base2 number 01101101 to base10 you just add the columns up:
(1x64) + (1x32) + (1x8) + (1x4) + (1x1) = 109 base10

top

Limitations & Solutions

Large Numbers
With base10, you can add as many columns as you like, and just increase the numbers size.
However with a computer you cant do that.
A computer needs a standard number of columns to represent a number - it uses 1byte (8bits, or 8columns) to represent a number.
If it didnt then it would get very confused, it wont know where one number starts and one number ends in the flow of electricity!

So, it allows 8 bits to represent a number, if a number is too large for a single byte, then it takes up 2 or more bytes!
Note, when the second byte starts, the power does not start again at 0, it continues from the end of the last byte!!!

Problem!
When a computer sends data from one pc to another it uses something called a 'protocol', this sets up a communication between the computers agreeing on the type of communication and things.
Now, not every binary number is stored in 8 bits as you will see when we look at bcd.
How does the computer know whether its a normal 8bit byte or bcd, how does it know when more than one byte is being used, how does it send fractions and negative numbers, and how does it send letters????????

Actually i dont know, if i find out ill put it here!

Note - everything a computer does is using binary, the binary code 01000001 means 65, but it also means the capital letter A !!!

Negative Numbers
The minimum value that can be stored in a single 8bit byte is 0, and the maximum (a 1 in every column) is 255.
We can increase the maximum by using multiple bytes, but what about negative numbers?

Negative numbers can be represented in two ways - Sign and Magnitude or Two's Complement.

Sign and Magnitude
The last bit (128's column) is now used to represent the sign, a 1 means negative, and a 0 means positive.
The range a single byte now has is -127 to +127.

If multiple bytes are being used only the very last bit is used for this purpose, not the last in each byte.

Twos Compliment
Here the last column instead of being 128, is replaced with -128 !!!
This makes things a little more complex but it makes arithmatic much easer later.
The range is -128 to +127.

Again, if multiple bytes are being used, only the very last bit is used for this purpose, not the last in each byte.

Fractions
How do you represent fractions?

In order to understand this you must understand twos compliment from above.

Okay, we have the fraction 23.45678
This can also be written as 0.2345678 x 102
We then store two parts, the number and the power.
The number 0.2345678 is called the 'mantissa' and the power 2 is called the 'exponent'.

To store the binary number 10111 as a fraction:
Change to 0.10111 x25 (exponent base10), then to 0.10111 x2101 (exponent base2).
You would then store the mantissa and exponent as 10111101 (in this case using 5 bits for the mantissa and 3 for the exponent).

However thats not quite right, you need to store both the mantissa and exponent in twos complement.
The first bit in both the mantissa and the exponent is a negative column, so for 10111 and 101 they would become 010111 and 0101.
And therefore it would become 0101110101 using 6 bits for the mantissa and 4 for the exponent.

If we wanted to store something like this: 0.00010101, we store 0.10101 as the mantissa and 1101 (base2 twos complement, -3 in base10) as the exponent.

We truncated the excess 0's at the beginning of the mantissa because it takes up less space to store the number of 0's youve removed, in the exponent, rather than storing the 0's themselves in the mantissa.
This also leaves more bits in the mantissa for actual data.
Doing this is called 'normalising', its done it order to store numbers as acurately as possible.

Increasing the length of the mantissa increases the accuracy of the stored number, increasing the length of the exponent increases the size of the stored number!