Binary

Where to start

In order to explain the binary number system, a good place to start is by comparing it with something you are already familiar with - the decimal number system.

In the decimal system we write numbers as thousands, hundreds, tens and units. Within the decimal number

4 1 5 6
the digit 4 represent 4 thousands
the digit 1 represent 1 hundreds
the digit 5 represent 5 tens
the digit 6 represent 6 units
Another way to say this is that each digit in a decimal number has 10 times the magnitude of the digit on its right. That is, the thousands digit is 10 times greater than the hundreds digit which is 10 times greater than the tens digit which is 10 times greater than the units

A useful way to look at this is:

unitsx 1
tensx 10
hundredsx 10 x 10
thousandsx 10 x 10 x 10
ten thousandsx 10 x 10 x 10 x 10
hundred thousandsx 10 x 10 x 10 x 10 x 10
millionsx 10 x 10 x 10 x 10 x 10 x 10

In maths there is a special notation for multiplying a number by itself many times, and the process is refered to as raising the number to a power.

e.g.

10 x 10 is the same as saying 10 to the power of 2, this is written as 102

10 x 10 x 10 is the same as saying 10 to the power of 3, this is written as 103

10 x 10 x 10 x 10 is the same as saying 10 to the power of 4, this is written as 104

10 on its own is the same as saying 10 to the power of 1, this is written as 101

In general the power that 10 is raised to is the number of 10's to be multiplied together, so 10N is N 10's multiplied together.

We can re-write the above table as:

unitsx 1 
tensx 10101
hundredsx 10 x 10102
thousandsx 10 x 10 x 10103
ten thousandsx 10 x 10 x 10 x 10104
hundred thousandsx 10 x 10 x 10 x 10 x 10105
millionsx 10 x 10 x 10 x 10 x 10 x 10106
NOTE: N0 is always 1 (take this on trust it's a maths thing) so the above table is completed as
unitsx 1100
tensx 10101
hundredsx 10 x 10102
thousandsx 10 x 10 x 10103
ten thousandsx 10 x 10 x 10 x 10104
hundred thousandsx 10 x 10 x 10 x 10 x 10105
millionsx 10 x 10 x 10 x 10 x 10 x 10106

Comparing the binary number system to the decimal number system

In the decimal number system a digit can have any value between 0 and 9 inclusive. In the binary number system a digit can only have the value 0 or 1.

So the number 4156 is a valid decimal number but an invalid binary number. The number 1010 is a valid decimal number and a valid binary number, but binary 1010 is not the same value as decimal 1010.

If there is any ambiguity as to which number system (also somtimes refered to as base or radix) we are using, then we should prefix the number with either the word binary or decimal or subscript it with 2 or 10 e.g. 10102 for binary 1010 or 101010 for decimal 1010

As previously explained, in the decimal number system each digit has 10 times the magnitude of the digit on its right. That is, the thousands digit is 10 times greater than the hundreds digit which is 10 times greater than the tens digit which is 10 times greater than the units

In contrast in the binary number system each digit has 2 times the magnitude of the digit on its right. So whereas we have thousands, hundreds, tens and units in the deciaml number system, we have sixteens, eights, fours, twos and units in the binary number system.

DECIMAL

         BNIARY

unitsx 1   unitsx 1
tensx 10   twosx 2
hundredsx 10 x 10   foursx 2 x 2
thousandsx 10 x 10 x 10   eightsx 2 x 2 x 2
ten thousandsx 10 x 10 x 10 x 10   sixteensx 2 x 2 x 2 x 2
hundred thousandsx 10 x 10 x 10 x 10 x 10   thirty twosx 2 x 2 x 2 x 2 x 2
millionsx 10 x 10 x 10 x 10 x 10 x 10   sixty foursx 2 x 2 x 2 x 2 x 2 x 2

As previously explained, in maths there is a special notation for multiplying a number by itself many times, and the process is refered to as raising the number to a power.

e.g.

2 x 2 is the same as saying 2 to the power of 2, this is written as 22

2 x 2 x 2 is the same as saying 2 to the power of 3, this is written as 23

2 x 2 x 2 x 2 is the same as saying 2 to the power of 4, this is written as 24

2 on its own is the same as saying 2 to the power of 1, this is written as 21

In general the power that 2 is raised to is the number of 2's to be multiplied together, so 2N is N 2's multiplied together.

We can re-write the above binary table as:

unitsx 1
twosx 2 21
foursx 2 x 2 22
eightsx 2 x 2 x 2 23
sixteensx 2 x 2 x 2 x 2 24
thirty twosx 2 x 2 x 2 x 2 x 2 25
sixty foursx 2 x 2 x 2 x 2 x 2 x 2 26
NOTE: N0 is always 1 (take this on trust it's a maths thing) so the above table is completed as
unitsx 1 20
twosx 2 21
foursx 2 x 2 22
eightsx 2 x 2 x 2 23
sixteensx 2 x 2 x 2 x 2 24
thirty twosx 2 x 2 x 2 x 2 x 2 25
sixty foursx 2 x 2 x 2 x 2 x 2 x 2 26
Again comparing the decimal and binary table but this time with emphesis on the powers of 10 and 2 for each digit
DECIMAL

         BNIARY

units 100   units 20
tens 101   twos 21
hundreds 102   fours 22
thousands 103   eights 23
ten thousands 104   sixteens 24
hundred thousands 105   thirty twos 25
millions 106   sixty fours 26
With decimal numbers when we append a 0 to a number we effectively multiply it by 10.
e.g.
4156
append 0 becomes
41560
With binary numbers when we append a 0 to a number we effectively multiply it by 2.
e.g.
10102 (which is equivalent to 1010)
append 0 becomes
101002 (which is equivalent to 2010)
With decimal numbers when we trim off the units digit we effectively divide it by 10.
e.g.
4156
trim off 6 becomes
415
With binary numbers when we trim off the units digit we effectively divide it by 2.
e.g.
10102 (which is equivalent to 1010)
trim off 0 becomes
1012 (which is equivalent to 510)

In general, if we append N 0's to a decimal number we effectively multiply it by 10N. If we append N 0's to a binary number we effectively multiply it by 2N. If we trim the N least significand digits from a decimal number we effectively divide it by 10N. If we trim the N least significand digits from a binary number we effectively divide it by 2N.

Most microprocessors have a special instruction that allows a binary 0 to be quickly appended to a binary number. This is usually refered to as left shift or left rotate instruction. Similarly they also have a right shift or right rotate instruction which allows the least significand bit to be trimmed off. The more powerfull microprocessors have instructions that can shift or rotate multiple bits per instruction.

XCSB has a left sift operator which looks like << and a right shift operator which looks like >>

Converting binary numbers to decimal numbers

The decimal number 1010 actually means

units0 x 1
tens1 x 10
hundreds0 x 10 x 10
thousands1 x 10 x 10 x 10

The binary number 1010 actually means

units0 x 1
twos1 x 2
fours0 x 2 x 2
eights1 x 2 x 2 x 2
So the binary number 1010 is actually equivalent to the decimal number 10

If we compare a binary number and a decimal number we must convert one of them so that they are of the same type, otherwise the comparision is meaningless.

To convert a binary number to decimal, we must convert each digit of the binary number into its decimal equivalent, then add together all the values we get.

Each of the bits within a byte has the following value
    27 26 25 24 23 22 21 20
This means that each bit translates to the equivalent decimal value of
   128   64   32   16   8   4   2   1 

To convert the binary number 100101 to decimal we would write it out as

 128   64   32   16   8   4   2   1 
0 0 1 0 0 1 0 1
Where we have a binary 1 we take the decimal value and place it in a list of number to be added together. The above would give us
 128   64   32   16   8   4   2   1 
0 0 1 0 0 1 0 1
  |  | | 
  |  | \-->    1 
  |  \---->    4 
  \------->  + 32 
              
               37 
So the binary number 100101 is equivalent to the decimal number 37

Converting decimal numbers to binary numbers

To convert a decimal number into its binary equivalent we must determin which powers of 2 make up the decimal number.

we do this by starting with a power of 2 larger than the number decimal number we wish to convert and try successive powers of 2 down to 1.

e.g.

converting decimal 75 to its binary equivalent

we start by writing out the power of 2 series which ends in a number greater than 75 (remember, 20 is always on the extream right and the powers increment towards the left)

27 26 25 24 23 22 21 20
 128   64   32   16   8   4   2   1 
Then we work from left to right trying to eliminate powers of 2 from the decimal number. As we discover which powers of 2 are contained in the decimal number, we mark these with a 1. All other powers of 2 that are not contained in the decimal number are marked with a 0.
    27 26 25 24 23 22 21 20
     128   64   32   16   8   4   2   1 
Step 1 75 < 128 so mark with 0
0
Step 2 75 >= 64 so mark with 1
and reduce 75 by 64 giving 11
0 1
Step 3 11 < 32 so mark with 0
0 1 0
Step 4 11 < 16 so mark with 0
0 1 0 0
Step 5 11 >= 8 so mark with 1
and reduce 11 by 8 giving 3
0 1 0 0 1
Step 6 3 < 4 so mark with 0
0 1 0 0 1 0
Step 7 3 >= 2 so mark with 1
and reduce 3 by 2 giving 1
0 1 0 0 1 0 1
Step 8 1 >= 1 so mark with 1
and reduce 1 by 1 giving 0
0 1 0 0 1 0 1 1
RESULT decimal 75 is equivalent to binary 0 1 0 0 1 0 1 1
NOTE: by the end of the conversion the decimal number must have reduced to 0 otherwise the conversion is incorrect
A further example shows how to cope with decimal numbers that reduce to 0 early on.

e.g.

converting decimal 96 to its binary equivalent
    27 26 25 24 23 22 21 20
     128   64   32   16   8   4   2   1 
Step 1 96 < 128 so mark with 0
0
Step 2 96 >= 64 so mark with 1
and reduce 96 by 64 giving 32
0 1
Step 3 32 >= 32 so mark with 1
and reduce 32 by 32 giving 0
0 1 1
Step 4 0 < 16 so mark with 0
0 1 1 0
Step 5 0 < 8 so mark with 0
0 1 1 0 0
Step 6 0 < 4 so mark with 0
0 1 1 0 0 0
Step 7 0 < 2 so mark with 0
0 1 1 0 0 0 0
Step 8 0 < 1 so mark with 0
0 1 1 0 0 0 0 0
RESULT decimal 96 is equivalent to binary 0 1 1 0 0 0 0 0
NOTE: although the decimal number has reduced to 0 by the end of step 3, the conversion dose not stop until all powers of 2 in the table have been applied.