Programming

Quick Binary Tutorial

People seem to freak out when they see binary.  School math classes come flooding back and their mind freezes up.  But there's no need for this at all.  Don't think of binary as being difficult, just thing of it as different!

Binary terminology

Binary is a number system based on switches called bits.  First, some terminology:

• Each bit is switch.
• Four bits is called a nibble.
• Eight bits is called a byte.

If the switch is on, the value of the switch is 1. If the switch off, the value of the switch is 0

Why people find binary "different"

There are few things about binary numbers that make them different to the plain vanilla decimal (base10) numbers that surround us every day.

1. Binary only uses two of the digits that we are used to - 0 and 1
2. We need to learn to read them right to left (like decimal the smallest or least significant bit is on the right)

So, step 1 to binary success is to accept point 1 and just accept point 2!

Understanding binary

In these examples we will work with 8-bit binary - that is bytes.  You can work with larger or smaller units but the byte is the most commonly used one.

OK, let's take a look at a byte of binary

01101101

OK, so the first thing to remember is to read the byte from right to left.  Let's make things easier by creating a table:

 Switch On = 1 Off = 0 0 1 1 0 1 1 0 1 Bit Position 8 7 6 5 4 3 2 1 Bit Value in decimal 128 64 32 16 8 4 2 1

Bit position is simply the position of the bit from the right - the bit furthest to the right is bit 1 and the bit on the left is number 8.

Decimal value of a bit is dependant on the position of the bit in the byte.  The decimal value of the first bit is 1, the second 2 and so on.  The eighth bit has a decimal value of 128.  Converting a byte into decimal is simply adding up the decimal values of each on bit in the byte.

Highlight all the on bits and ignore the off bits:

 Switch On = 1Off = 0 0 1 1 0 1 1 0 1 Bit Position 8 7 6 5 4 3 2 1 Bit Value in decimal 128 64 32 16 8 4 2 1 01101101 = 64 + 32 + 8 + 4 + 1

Bit 8 =   0

Bit 7 =  64

Bit 6 =  32

Bit 5 =   0

Bit 4 =   8

Bit 3 =   4

Bit 2 =   0

Bit 1 =   1

Take out the zeros and add together all the decimal values of the on bits to the the value of the byte in decimal.

01101101 = 64 + 32 + 8 + 4 + 1 = 109

Easy!

Let's try another example.

10111001

First, create a table (do this until you are comfortable working with binary numbers).

 Switch On = 1Off = 0 1 0 1 1 1 0 0 1 Bit Position 8 7 6 5 4 3 2 1 Bit Value in decimal 128 64 32 16 8 4 2 1

Now highlight all the on bits and ignore the off bits:

 Switch On = 1Off = 0 1 0 1 1 1 0 0 1 Bit Position 8 7 6 5 4 3 2 1 Bit Value in decimal 128 64 32 16 8 4 2 1 10111001 = 128 + 32 + 16 + 8 + 1

Bit 8 = 128

Bit 7 =   0

Bit 6 =  32

Bit 5 =  16

Bit 4 =   8

Bit 3 =   0

Bit 2 =   0

Bit 1 =   1

Take out the zeros and add together all the decimal values of the on bits to the the value of the byte in decimal.

10111001 = 128 + 32 + 16 + 8 + 1 = 185

Here are a few for you to practice on!

1. 11110111
2. 00010010
3. 10101010
4. 11111111
5. 11001100

Here is a table to help you!

 Switch On=1 Off=0 On/Off 0 1 On/Off 0 1 On/Off 0 1 On/Off 0 1 On/Off 0 1 On/Off 0 1 On/Off 0 1 On/Off 0 1 Bit Position 8 7 6 5 4 3 2 1 Bit Value in decimal 128 64 32 16 8 4 2 1

And this might be useful too!

Bit 8 =

Bit 7 =

Bit 6 =

Bit 5 =

Bit 4 =

Bit 3 =

Bit 2 =

Bit 1 =