Binary etc.   
Copyright: Tony Drewry
The Decimal system, The Octal  system  Hexadecimal, The Binary  system
The relationship between Hex, Octal and Binary Converting from Decimal to Hex, Octal, and Binary
The principle operations of the computer  -  Adding bitsComparing bitsMoving bits
Subtraction - 2's Complement Some questions

The Decimal system

Normally, we count in 10s.  This is the Decimal system, also called the Denary system
The system can be visualised as columns, or placings for digits representing particular values.
For example, in Decimal:  
the number '2002' represents: 2 x 10 x10 x10
0 x 10 x10 
0 x 10 units
0 units
1000s

2

100s

0

10s

0

1s

2

the number '159' represents: 
1 x 10 x10
5 x 10 units
9 units
0

1

5

9
The system is based on 10s, but a '10' as a whole number can never appear in a column - only the digits 0to9, since as soon as we reach 10, we carry 1 to the next left-hand column.


The Octal  system

The Octal  system is based on 8s.
Again we can  visualise the system as columns.  However, in the octal system only  the digits 0 to 7 may appear in the columns. (The highest possible value in any column is 7, because when we reach 8 in a column we carry 1 to the next left-hand column. In this system, the digits 8 and 9 cannot be used.)
 
For example: in Octal
 decimal '2002' is  3722
3 x 8 x 8 x 8
7 x 8 x 8
2 x 8 units
0 units
512s

3

64s

7

8s

2

1s

2

decimal '159'  is  237 2 x 8 x 8
3 x 8 units
7 units
0
2 3 7


The Hexadecimal  system (Hex) is based on 16s.
Visualise the system as columns and a problem should become apparent.
We can only have single digits in any given column, but we have to be able to count up to 16 before carrying 1 to the next column.
In the Hexadecimal  system:
the letter A is used to represent 10.  Then B = 11, C = 12, D = 13, E = 14 and F = 15.
For example: in Hex
 decimal '2002' is  7D2  0 x 16 x 16 x 16
7 x 16 x 16
13 x 16 units
0 units
4096s

0

256s

7

16s

D

1s

2

decimal '159'  is  9F 0 16 x 16
9 16 units
15 units
0
0 9 F


The Binary system is based on 2s.
Again we can  visualise the system as columns, or placings for digits representing particular values.
 
For example:
 In binary 210 29 28 27 26 25 24 23 22 21 20
decimal '2002
is 11111010010
1024
1
512
1
256
1
128
1
64
1
32
0
16
1
8
0
4
0
2
1
1
0
decimal '159
is 10011111
0 0 0 1 0 0 1
1
1 1 1
As you can see, each column can only contain the digits 0 or 1.
In a computer we call binary digits BITS; i.e. Binary digITS.  The Binary system is an ideal counting system for machines.
0's and 1's can be represented in a machine very easily, and in a number of ways. For example: (See also 'Principle operations', below)

The relationship between Hex, Octal and Binary
The diagram below shows the relationship between Hex (base 16) and Binary (base 2), and between Octal (base 8) and Binary.  Observe that to convert binary into either hex or octal is very straightforward if you can read 3 and 4 digit numbers in binary.

In the case of translating to Octal one must be able to count up to 7 in binary; i.e. in the example above  one needs to know that 011 in binary  = 3 decimal, that binary 111 = 7decimal, and that binary 010 = 2 in decimal.
Similarly, to write binary as hex, one only needs to know how to count up to 15 in binary.
If you had to write machine code which would you use? Binary, Octal or Hex?


Converting from decimal
 The following diagrams illustrate the division method of manually converting from decimal to base 2 (binary), base 8 (octal) and base 16 (hex).
 
Converting decimal 2002 into binary

Converting decimal 159 into binary
 



 
Converting decimal 2002 into octal and hex


 


The principle operations of the computer are based on its ability to add bits,compare bits and move bits.
All computer functions are based on combinations of these micro operations. Presented on this page the micro operations may appear tedious and a rather slow way of doing things - just remember that in the time that you have taken to read this sentence a computer can do 10 billion (or more) additions, comparisons and moves.

Adding bits
 

0 + 0 = 0 0 + 1 = 1 1 + 1 = 0   (carry 1)
hence:
 0000
+0000
=0000
 0001
+0000
=0001
 0001
+0100
=0101
 0001
+0001
=0010
 0010
+0010
=0100
 0110
+0011
=1001
  1111
+ 1111
=11110

(see also 'How a computer adds' for some diagrams illustrating the mechanics of the operation)

Comparing bits
 

0 = 0 ? result  is  1 0 = 1 ? result is  0 1 = 1 ? result is  1

When 2 bits are compared the answer is always a bit (a 1 or a 0). This ability to compare bits allows us to use computers to perform logical operations.

Moving bits

There are two type of movement: (a) from one part of memory to another; and (b)  a shift.
It is the second form of move, the 'shift' which is of interest to us here. For example:
if the computer has the following binary number stored in memory:

0 0 1 1 0 1 0 1
the number can be doubled simply by shifting each bit one cell to the left and inserting a 0 in the rightmost cell:
0 1 1 0 1 0 1 0

Shifting bits leftwards supports very fast even-number multiplication.
Shifting bits plus adding bits provides odd-number multiplication.
Shifting bits rightwards provides division.

For example, 
taking the number 42 as a starting point:
1 0 1 0 1 0 0 0 0 0 . 672
1 0 1 0 1 0 0 0 0 . 336
1 0 1 0 1 0 0 0 . 168
1 0 1 0 1 0 0 .  84
0 1 0 1 0 1 0 .  42
0 0 1 0 1 0 1 . 0  21
0 0 0 1 0 1 0 . 1 0  10.5
0 0 0 0 1 0 1 . 0 1 0   5.25
0 0 0 0 0 1 0 . 1 0 1 0   2.625
Subtraction using complements

It is a curious fact that a computer subtracts using addition. First a method  known as 2's Complement is used to create a negative representation of the number to be subtracted..
Then subtraction is a matter of adding the negative number to the number from which it is being subtracted. (!)
See 2's Complement


Some questions

Conventionally we usually talk in terms of bytes - or collections of 8 bits. This allows a variety of items to be transmitted. For example, the most common way of representing alphabetical text inside a computer is through the use of ASCII codes. Every character on a conventional keyboard has a code which is an 8-bit number (a byte).

Why do we use that particular number of bits? We need to be certain that we have enough variety in the codes to be able to cope with all the possible characters. (Note that itís necessary to keep the number of bits in powers of two.)

1.  If we have 8-bit binary numbers, what is the maximum number of codes available?
2.  Is this number enough to cope with the total number of characters on the keyboard?
3.   Would 4-bit numbers be enough to cope with the total number of characters on the keyboard?

If each machine cycle (instruction to the CPU) needs a byte when text is dealt with, what happens if the amount of information increases? e.g. What about non-text information?

Much of what is displayed by computers today is in the form of images, or other forms of output such as sound. What does this do to the amount of digital data that must be transferred? (Or, to put it another way, why are graphics so much harder to download on the World Wide Web than text?) To illustrate:

4.  A screen display allows 64 colours with 8 levels of brightness.
i) How many items of information must the CPU be able to cope with so that all colours and levels of brightness are available?
ii) What size of binary number will the CPU need to be able to handle so that all colours and levels of brightness are available? (i.e. what's the answer to the above question in binary?)
iii) If the number of colours is reduced to 50, would that reduce the size of the binary numbers the CPU needs to cope with?
(Note - conventional screen displays now allow for at least 256 colours.)
iv) If we use a 6 digit hex code to provide a numbering system for colours, how many colours could be identified using that code?
(Actually, a 6 digit hex code is commonly used to identify colours. See some examples.)

5. What about sound? What kinds of variation would be needed to output music?



Tony Drewry, September.  2002
Tony's Home Page