## Introduction

Because of the ease with each binary logic elements are manufactured and because of their remarkably low price, it was inevitable that the binary number system was chosen to represent numerical data within a digital computer.

This blog examines how numbers are represented in digital form, how they are converted from `base`

to another, and how they are manipulated within the computer. We begin with an examination of binary codes in general and demonstrate how patterns of ones and zeros can represent a range of different quantities.

### Bits, bytes, words, and characters

The smallest quantity of information that can be stored and manipulated inside a computer is the `bit`

, which can take the values of 0 and 1. Digital computers store information in the form of groups of bits called `words`

. The number of bits per word varies from computer to computer. A group of 8 bits has come to be known as `byte`

. Today's microprocessors and minicomputers are byte-oriented with word lengths that are integer multiples of 8 bits (i.e. their data elements and addresses are 8,16,32 or 64 bits).

A word is spoken of as being 2,4, or 8 bytes long, because its bits can be formed into two, four, or eight groups of 8 bits, respectively.

An `n-bit`

word can be arranged into `2^n`

unique bit patterns. Example:

```
n = 1, 2, 3, and 4
```

Bits(n) | Patterns 2^n | Values |

1 | 2 | 0,1 |

2 | 4 | 00,01,10,11 |

3 | 8 | 000,001,010,011,100,101,110,111 |

4 | 16 | ? (I will leave that to you) |

So, what do the `n-bits`

of a word represent? absolutely nothing because the is no intrinsic meaning associated with a pattern of 1s and 0s.

The following are some of the entities that a word may represent:

**An instruction** or operation to be performed by the CPU is represented by a binary pattern such as 000100100010001011100010111.

**A numeric quantity** either alone or a sequence of words, may represent a numerical quantity.

A `character`

The alphanumeric characters (Aa-Zz, 0-9, and symbols ,-.+!? etc). are assigned binary patterns so that they can be stored and manipulated within the computer.

The ASCII code (American Standard Code for Information Interchange) is widely used throughout the computer industry to encode alphanumeric characters. The ASCII code represents a character by 7 bits, allowing a maximum of `2^7 = 128`

different characters. Checkout the ascii table at: ascii table

The 7-bit ASCII code has been extended to the 8-bit (ISO 8859-1) Latin code to add accented characters such as á, é, í, ó, ú, ý, Á, É, Í, Ó, Ú, Ý. Although suited to Europe and the USA, Latin code cannot deal with many of the World's languages.

A 16-bit cod, called `unicode`

has been designed to represent the characters of most of the World's written languages such as Chinese and Japanese. The programming language Java has adopted Unicode as the standard means of character representation.

### Number bases

The place value of a digit in a number determines its value at that position. In the standard decimal numbers, the value of a place is ten times the value of the place on its right. The decimal number system is also called base-10, which is composed of digits from 0 to 9. Let's say that position 1 is at the right-most digit of the whole number, as follows:

```
2018
Place value at position 1 is 1 multiplied by 8 represents 8.
Place value at position 2 is 10 multiplied by 1 represents 10.
Place value at position 3 is 100 multiplied by 0 represents 0.
Place value at position 4 is 1000 multiplied by 2 represents 2000.
```

The sum of all represented numbers is the actual value. Following this concept will help us to read or convert into other number bases.

In base-2 numbers, the value of a place is 2 times the value of the place on its right. Base-2 uses only 2 digits, composed of 0 1

We will append a small b to denote that the number is of base-2 format.

Base-2 numbers are also called binary numbers, each digit in a binary string is called a bit.

```
11010b
Place value at position 1 is 1 multiplied by 0 represents 0.
Place value at position 2 is 2 multiplied by 1 represents 2.
Place value at position 3 is 4 multiplied by 0 represents 0.
Place value at position 4 is 8 multiplied by 1 represents 8.
Place value at position 5 is 16 multiplied by 1 represents 16
The equivalent decimal value of 11010b is 26
```

In base-16 numbers, the value of a place is 16 times the value of the place on its right. It is composed of digits 0 to 9 and letters A to F where `A==10`

, `B==11`

, `C==12`

, `D==13`

, `E==14`

, and `F==15`

Base-16 is also known as hexadecimal numbers denoted with letter h Hexadecimal numbers can also instead be prefixed with `0x`

```
BEEFh
Place value at position 1 is 1 multiplied by 0Fh (15) represents 15.are
Place value at position 2 is 16 multiplied by 0Eh (14) represents 224.
Place value at position 3 is 256 multiplied by 0Eh (14) represents 3584.
Place value at position 4 is 4096 multiplied by 0Bh (11) represents 45056.
The equivalent decimal value of BEEFh is 48879.
```

#### Converting between bases

Converting base-10 into other bases simply requires division of the base being converted into, while taking of the reminders.

```
87 to base-2
87 divides by 2 is 43 reminder 1
43 divided by 2 is 21 remainder 1.
21 divided by 2 is 10 remainder 1.
10 divided by 2 is 5 remainder 0.
5 divided by 2 is 2 remainder 1.
2 divided by 2 is 1 remainder 0.
1 divided by 2 is 0 remainder 1.
and nothing more to divide since we're down to 0.
base-2 has digits 0 and 1.
Writing the remainders backward results to 1010111b.
```

The following is an example for base-16:

```
34512 to base-16
34512 divided by 16 is 2157 remainder 0.
2157 divided by 16 is 134 remainder 13 (0Dh)
134 divided by 16 is 8 remainder 6.
6 divided by 16 is 0 remainder 6.
base-16 has digits from 0 to 9 and A to F.
Writing the remainders backward results to 66D0h
```