Part One: Hardware world

Chapter 1: Data representation on computer hardware

## 1.2) The binary representation of numeric data:

We have known that to speak with a computer, we need to know how to convert our language to binary language (i.e. to zeros and ones).

So, let’s start by learning how to convert numbers to zeros and ones.

### 1.2.1) Number systems:

When we say that we will start learning how to convert numbers to zeros and ones, you will think that we will convert decimal numbers to zeros and ones.

Yes. You are right. But you must consider 2 points:

• The binary language has only 2 numbers which are called a binary number system.
• We will not take conversion from decimal number system to binary number system only, but you will see that many other number systems are used in computer science.

There are many number systems. You can see a list of them through this link (Click here).

But we only care about the number systems found on the next table:

Decimal100,1,2,3,4,5,6,7,8,9
Binary20,1
Ternary30,1,2
Octal80,1,2,3,4,5,6,7
Hexadecimal160,1,2,3,4,5,6,7,8,9, A, B, C, D, E, F

Let me explain this table.

In this table, you can see 5 number systems.

The word “base” means how many numbers does the number system contain? So, the decimal number system is called decimal because its base is 10 which means it consists of 10 numbers: 0 to 9.

The same is in the case of the binary system. Its base is 2 because it has 2 numbers: 0 and 1.

Also, the base of the ternary number system is 3 because it has 3 numbers: 0 to 2.

The base of the octal number system is 8 because it has 8 numbers: 0 to 7.

However, in the case of the hexadecimal number system, the base is 16 which means it has 16 numbers: 0 to 15.

The problem is that 15 is not one digit. 15 is a number that consists of 2 digits.

The difference between a digit and a number is that a digit consists of only one symbol; 0 to 9, while the number consists of more than one symbol, or let’s say digit.

So, we cannot consider 15 as a single-digit inside the hexadecimal numbers system. To solve this problem, we denote letters for numbers from 10 to 15, as follows:

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

So, are we able to convert from any number system to binary or decimal number systems? Of course. You can convert from any number system to another. This is what you will see in this chapter.

However, to be able to convert from any number system to another, you need to know the general rule by which any number system works.

We can extract this general rule from the decimal number system. We will call this general rule the base-n number system rule.

#### 1.2.1.1) Decimal number system:

First, we agreed that the decimal number system has 10 numbers: 0 to 9, hence the name decimal. Also, we can call it the base-10 number system.

1. ##### How decimal number system works?

Let’s take an integer as an example. Say 159. This number consists of 3 digits; 1, 5, and 9 (We have known the difference between number and digit).

Each digit has a quantity according to its position. Let’s see the next table: The index represents the position of each digit. Notice that we start numbering the positions of right to left.

As you can see, digit 9 is in the position (i.e., index 0) associated with the value 1, digit 5 is in position (i.e., index 1) associated with the value 10. and digit 1 is in the position (i.e., index 2) associated with the value 100.

To illustrate that, we can write the following: As you can see, each quantity equals the quantity of its right quantity multiplied by 10.

We can get the value of the entire expression by multiplying the value of each digit by the quantity associated with the digit’s position and then adding those products.

We can rewrite this more technically as follows: You can see that the value is not changed. So, we can rewrite the previous table as follow: As you can see, the quantity of each digit equals the base of the number system raised to the power of the index of the digit.

So, what about the fractional part? let’s add a fractional part to the previous example. Say 159.25.

In the fractional part, we will change the index. We start numbering the index from -1 (not zero) and continue numbering from left to right.

Why the index is negative in the fractional part? Because the fractional part results from the division by 10 or multiple of ten.

So, in the fractional part, each quantity equals the quantity of its left quantity divided by 10.

So, we can rewrite the previous table as follows: Also, we can rewrite the technical notation as follows: Did you get it? What can we conclude from this?

• Each digit is multiplied by 10 raised to the power of the digit’s index.
• The index of the integer part starts from 0 and continues from right to left. The index of the fractional part starts from -1 and continues from left to right.
• In the integer part, each quantity equals the quantity of its right quantity multiplied by 10. In the fractional part, each quantity equals the quantity of its left divided by 10.

Why 10?

It is the base of the decimal number system, which means it has 10 numbers: 0 to 9.

1. ##### Conclusion of the general rule by which any number system works

Let’s extract a general algorithm by which any number system works.

• We multiply each digit by its quantity. This quantity equals the base of the number system raised to the power of the digit’s index (let’s denote (i) to index). (d) is for digit.

• Then, we obtain the value (x) of the entire expression by adding the products of step one.

####  1.2.1.2) Binary number system:

1. ##### Applying the general algorithm on the binary number system to convert from decimal number system to binary number system:

Let’s apply the general algorithm to the binary.

When you apply the general algorithm to any number system, you need to know the base of the number system that you apply the algorithm to it.

Here, we apply the general algorithm to the binary number system. The base of the binary numbers system is 2 because it has only 2 numbers; 0 and one, which means that ( n=2 ).

So, we can write the following: so, let’s simplify the table as follows: You can replace (d) with the digits of the number system. The binary number system has only 2 digits. So, d={0,1}.

Let’s take examples

Example 1:

What is the equivalent decimal number of the following binary number (100)2?  Notice that we wrote 2 as a subscript to the number to illustrate that 100 is a binary number, not a decimal number. The same in the case of (4)10. The 10 illustrates that 4 is a decimal number.

Example 2: A general and simple rule to convert from decimal number system to binary number system as follows:

From the previous example, we can extract a general and simple algorithm to convert from decimal number system to binary number system as follows:

• Write the quantity of each digit above it as a base (2) raised to the digit’s index, or by writing 1, starting from the rightmost digit, and multiply each quantity by to as you go from right to left.

…8 4 2 1

• Get the summation of the quantities associated with positions occupied by ones.

Let’s take an example.

Example 3:

What is the equivalent decimal number of the following binary number (1110)2? The first step is to write the quantities above the numbers, as shown above. Notice that we start by writing 1 above the rightmost digit, then multiply by 2 as we go from right to left. From the previous example, we say that (100)2 is the bit pattern of (4)10 in the decimal number system, (110)2 is the bit pattern of (6)10, and (1110)2 is the bit pattern of (14)10.

Notice that the quantities of the digits are called weights.

So, what if there is a fractional part in the binary number? Let’s see an example.

Example 4:

What is the equivalent decimal number of the following binary number (1110.01)2? You can simply by using the following table: But notice that the computer does not store the decimal point as a dot in the memory. We use it here to learn how to convert from decimal to binary on paper, but there are methods to store the fractional part in memory. We will cover these methods in the course.

Watch Our video: