     ## Binary Numbers

### Introduction

Binary is a number system that only uses two different numbers, these are 0 and 1.

In an electronic circuit the number 0 and the number 1 can easily be represented by a voltage being OFF or ON. Binary is ideal for use with digital circuits which are either ON or OFF. In a digital circuit, ON represents Binary 1 and OFF represents Binary 0.

The binary characters 0 and 1 are called bits. A number of bits make a binary word. For example, the binary number “0 1 1 0” is a 4 bit binary word. An 8 bit binary word is a special case and is called a byte.

### 1 Bit Binary

The first two numbers, Zero and One, in our usual number system (decimal) are shown below:

Decimal Zero is represented by binary 0, Decimal One is represented by binary 1 ... or in shorthand:
0 → 0 ... zero lots of 1
1 → 1 ... one lot of 1

### 2 Bit Binary

To represent the decimal numbers Two and Three, more digits are needed. To do this there is a second column in binary. The first (right hand) column represents a quantity of 1 (unity). The second (left hand) column represents a quantity of 2 (21). Therefore:

0 → 0 0 ... zero lots of two and zero lots of 1 (0×2 + 0×1)
1 → 0 1 ... zero lots of two and one lot of 1 (0×2 + 1×1)
2 → 1 0 ... one 2 and zero lots of 1 (1×2 + 0×1 = 2)
3 → 1 1 ... one 2 and one 1 (1×2 + 1×1 = 2 + 1 = 3)

### 3 Bit Binary

To represent the next decimal numbers (Four to Seven) another column is required on the left hand side. This column represents the quantity 4 (22).

Therefore the first, right hand, column represents the number of 1s, the second column represents 2s and the third, left hand, column represents the number of 4s:

4 → 1 0 0 ... one 4, zero 2s and zero 1s
5 → 1 0 1 ... one 4, zero 2s and one 1 (1×4 + 0×2 + 1×1 = 4 + 1 = 5)
6 → 1 1 0
7 → 1 1 1

Three columns can represent eight decimal numbers (0 → 7) because there are eight different combinations of 1s and 0s in three columns. Notice 23 = 8.

### 4 Bit Binary

It would be better if all the binary numbers had the same number of digits (bits) and four bits are needed to count to 9. The right hand bit is called the Least Significant Bit (LSB) and is worth 1. The left hand bit is called the Most Significant Bit (MSB) and is worth 8.

The first ten decimal numbers are:

0 → 0 0 0 0
1 → 0 0 0 1
2 → 0 0 1 0
3 → 0 0 1 1
4 → 0 1 0 0
5 → 0 1 0 1
6 → 0 1 1 0
7 → 0 1 1 1
8 → 1 0 0 0 ... one 8 and nothing else
9 → 1 0 0 1

Not all of the combinations have been used yet because 24 = 16 and so, with four bits, 16 numbers can be represented, 0 to 15.

10 → 1 0 1 0
11 → 1 0 1 1
12 → 1 1 0 0 ... one 8 and one 4
13 → 1 1 0 1
14 → 1 1 1 0
15 → 1 1 1 1 ... 1×8 + 1×4 + 1×2 + 1×1 = 15

The biggest number represented is 15 because to represent the decimal number 16 requires a 5th bit. 25 = 16 so the 5th bit is worth 16.

### More Bits

Bigger numbers are represented by more bits. Each bit is worth twice the previous bit. Therefore the first few bits in a binary number are worth:

1st bit → 20 = 1
2nd bit → 21 = 2
3rd bit → 22 = 4
4th bit → 23 = 8
5th bit → 24 = 16
6th bit → 25 = 32
7th bit → 26 = 64
8th bit → 27 = 128
9th bit → 28 = 256
10th bit → 29 = 512
11th bit → 210 = 1024

For example, an 8 bit binary number can represent 256 different decimal values which are the numbers 0 to 255.

In binary the maximum 8 bit binary word is 1 1 1 1 1 1 1 1

The binary word 1 1 1 1 1 1 1 1 is equivalent to 1×128 + 1×64 + 1×32 + 1×16 + 1×8 + 1×4 + 1×2 + 1×1 = 255

### Converting Decimal to Binary

First decide how many bits are necessary and then decide which bits are necessary to add up to the desired decimal number.

Example: What is decimal 27 in binary?
Solution: The 6th bit is worth 32 and that is more than needed therefore use 5 bits.
5th bit → 16. Is it needed? Yes. 27 − 16 = 11 remaining
4th bit → 8. Is it needed for the remaining 11? Yes. 11 − 8 = 3 remaining
3rd bit → 4. Is it needed? No, only 3 remaining, don't need a 4
2nd bit → 2. Is it needed for 3? Yes. 3 − 2 = 1 remaining
1st bit → 1. Is it needed? Yes

Therefore 27 → 1 1 0 1 1
(1×16 + 1×8 + 0×4 + 1×2 + 1×1)

Example: What is 42 in binary?
Solution: 6 bits are required. 7th bit → 64 which is too big.
1 × 32 ... 42 − 32 = 10
0 × 16
1 × 8 ... 10 − 8 = 2
0 × 4
1 × 2 ... 2 − 2 = 0
0 × 1

Therefore 42 → 1 0 1 0 1 0

Note, sometimes it is necessary to represent numbers as 8 bit binary numbers. In this case just add extra zeros to the left hand side as these don't add any value:

27 → 0 0 0 1 1 0 1 1
42 → 0 0 1 0 1 0 1 0

### Converting Binary to Decimal

To convert a binary number to decimal, add up each digit that is none zero.

Example: What is 0 1 1 1 0 0 0 1 in decimal?
Solution: The MSB is worth 128 as this is an 8 bit binary number (27 = 128)
From the left we have:
0×128 + 1×64 + 1×32 + 1×16 + 0×8 + 0×4 + 0×2 + 1×1 = 113

0 1 1 1 0 0 0 1 → 113