2.1 INTRODUCTION
A number system (or numeral system) defines how a number can be represented using distinct symbols. A number can be represented differently in different systems. For example, the two numbers (2A)16 and (52)8 both refer to the same quantity, (42)10, but their representations are different.
As we use symbols (characters) to represent words in a natural language, we use symbols (digits) to represent numbers. However, we know that the number of symbols (characters) in any language is limited. We need to repeat characters and combine them to create words. It is the same for numbers: we have a limited number of symbols (digits) to represent numbers, which means that the digits need to be repeated and combined.
Several number systems have been used in the past and can be categorized into two groups: positional systems and non-positional systems. Our main goal is to discuss the positional number systems, but we also give examples of non-positional systems.
2.2 POSITIONAL NUMBER SYSTEMS
In a positional number system, the position a symbol occupies in the number determines the value it represents. In this system, a number represented as:
has the value of:in which S is the set of symbols, b is the base (or radix), which is equal to the total number of the symbols in the set S, and SK and SL are symbols in the whole and fraction parts of the number.
Note that we have used an expression that can be extended from the right or from the left. In other words, the power of b can be 0 to K - 1 in one direction and -1 to -L in the other direction. The terms with non negative powers of b are related to the integral part of the number, while the terms with negative power of b are related to the fractional part of the number. The +- sign shows that the number can be either positive or negative.
2.2.1 The decimal system (base 10)
The first positional number system we discuss is the decimal system. The word decimal is derived from the Latin root decem (ten). In this system the base b = 10 and we use ten symbols to represent a number. The set of symbols is S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. As we know, the symbols in this system are often referred to as decimal digits or just digits. We use +- to show that a number can be positive or negative, but remember that these signs are not stored in computers—computers handle the sign differently.
In the decimal system for simplicity, we often drop the parentheses, the base, and the plus sign if the number is positive. For example, we write the number (552.23)10 as 552.23—the base and plus signs are implicit.
Integers
An integer (an integral number with no fractional part) in the decimal system is familiar to all of us—we use integers in our daily life. In fact, we have used them so much that they are intuitive. We represent an integer as +- SK–1 … S1 S0. The value is calculated as:
in which Si is a digit, b = 10 is the base, and K is the number of digits.
Another way to show an integer in a number system is to use place values, which are powers of 10 (100, 101, …, 10K-1) for decimal numbers. Figure 2.1 shows an integer in the decimal system using place values.
Example 2.1
The following shows the place values for the integer +224 in the decimal system:
Example 2.2
The following shows the place values for the decimal number –7508. We have used 1, 10, 100, and 1000 instead of powers of 10:
Maximum value
Sometimes we need to know the maximum value of a decimal integer that can be represented by K digits. The answer is Nmax = 10K - 1. For example, if K = 5, then the maximum value is Nmax = 105 - 1 = 99 999.
Reals
A real (a number with a fractional part) in the decimal system is also familiar. For example, we use this system to show dollars and cents ($23.40). We can represent a real as +- SK–1 … S1 S0 . S–1 … S–L. The value is calculated as:
Example 2.3
The following shows the place values for the real number +24.13:
2.2.2 The binary system (base 2)
The second positional number system we discuss is the binary system. The word binary is derived from the Latin root bini (or two by two). In this system the base b = 2 and we use only two symbols, S = {0, 1}. The symbols in this system are often referred to as binary digits or bits (binary digit). As we will see, data and programs are stored in the computer using binary patterns, a string of bits. This is because the computer is made of electronic switches that can have only two states, on and off. The bit 1 represents one of these two states and the bit 0 the other.
Integers
We can represent an integer as +- (SK–1 … S1 S0)2. The value is calculated as:
in which Si is a digit, b = 2 is the base, and K is the number of bits. Another way to show a binary number is to use place values (20, 21, … 2K-1). Figure 2.2 shows a number in the binary number system using place values:Example 2.4
The following shows that the number (11001)2 in binary is the same as 25 in decimal. The subscript 2 shows that the base is 2:
Maximum value
The maximum value of a binary integer with K digits is Nmax = 2K – 1. For example, if K = 5, then the maximum value is Nmax = 25 – 1 = 31.
Reals
A real—a number with an optional fractional part—in the binary system can be made of K bits on the left and L bits on the right, +- (SK–1 … S1 S0 . S–1 … S–L)2. The value can be calculated as:
in which Si is a bit, b = 2 is the base, K is the number of bits to the left, and L is the number of bits to the right of the decimal point. Note that K starts from 0, but L starts from -1. The highest power is K - 1 and the lowest power is -L.Example 2.5
The following shows that the number (101.11)2 in binary is equal to the number 5.75 in decimal:
2.2.3 The hexadecimal system (base 16)
Although the binary system is used to store data in computers, it is not convenient for representation of numbers outside the computer, as a number in binary notation is much longer than the corresponding number in decimal notation. However, the decimal system does not show what is stored in the computer as binary directly—there is no obvious relationship between the number of bits in binary and the number of decimal digits. Conversion from one to the other is not fast, as we will see shortly.
To overcome this problem, two positional systems were devised: hexadecimal and octal. We first discuss the hexadecimal system, which is more common. The word hexadecimal is derived from the Greek root hex (six) and the Latin root decem (ten). To be consistent with decimal and binary, it should really have been called sexadecimal, from the Latin roots sex and decem. In this system the base b = 16 and we use 16 symbols to represent a number. The set of symbols is S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}. Note that the symbols A, B, C, D, E, F (uppercase or lowercase) are equivalent to 10, 11, 12, 13, 14, and 15 respectively. The symbols in this system are often referred to as hexadecimal digits.
Integers
We can represent an integer as +- SK–1 … S1 S0. The value is calculated as:
in which Si is a digit, b = 16 is the base, and K is the number of digits. Another way to show a hexadecimal number is to use place values (160, 161, …, 16K-1). Figure 2.3 shows a number in the hexadecimal number system using place values.
Example 2.6
The following shows that the number (2AE)16 in hexadecimal is equivalent to 686 in decimal:
Note that the value in the decimal system is N = 512 + 160 + 14 = 686.
Maximum value
The maximum value of a hexadecimal integer with K digits is Nmax = 16K – 1. For example, if K = 5, then the maximum value is Nmax = 165 – 1 = 1 048 575.
Reals
Although a real number can be also represented in the hexadecimal system, it is not very common.
2.2.4 The octal system (base 8)
The second system that was devised to show the equivalent of the binary system outside the computer is the octal system. The word octal is derived from the Latin root octo (eight). In this system the base b = 8 and we use eight symbols to represent a number. The set of symbols is S = {0, 1, 2, 3, 4, 5, 6, 7}. The symbols in this system are often referred to as octal digits. Integers We can represent an integer as +- SK–1 … S1 S0. The value is calculated as:
in which Si is a digit, b = 8 is the base, and K is the number of digits. Another way to show an octal number is to use place values (80, 81, …, 8K-1). Figure 2.4 shows a number in the octal number system using place values.Example 2.7
The following shows that the number (1256)8 in octal is the same as 686 in decimal:
Note that the decimal number is N = 512 + 128 + 40 + 6 = 686.
Maximum Value
The maximum value of an octal integer with K digits is Nmax = 8K − 1. For example, if K = 5, then the maximum value is Nmax = 85 − 1 = 32767.
Reals
Although a real number can be also represented in the octal system, it is not very common.
2.2.5 Summary of the four positional systems
Table 2.1 shows a summary of the four positional number systems discussed .
Table 2.2 shows how the number 15 is represented with
two digits in decimal,
four digits in binary,
two digits in octal, and
only one digit in hexadecimal.
The hexadecimal representation is definitely the shortest.
2.2.6 Conversion
We need to know how to convert a number in one system to the equivalent number in another system. Since the decimal system is more familiar than the other systems, we first show how to covert from any base to decimal. Then we show how to convert from decimal to any base. Finally, we show how we can easily convert from binary to hexadecimal or octal and vice versa.
Any base to decimal conversion
This type of conversion is easy and fast. We multiply each digit with its place value in the source system and add the results to get the number in the decimal system. Figure 2.5 shows the idea.
Example 2.8
The following shows how to convert the binary number (110.11)2 to decimal: (110.11)2 = 6.75:
Decimal: 6.75
Example 2.9
The following shows how to convert the hexadecimal number (1A.23)16 to decimal:
Decimal: 26.137
Note that the result in the decimal notation is not exact, because 3 x 16-2 = 0.01171875. We have rounded this value to three digits (0.012). In other words, (1A.23)16 ≈ 26.137. When we convert a number in decimal to hexadecimal, we need to specify how many digits we allow to the right of the decimal point.
Example 2.10
The following shows how to convert (23.17)8 to decimal:
This means that (23.17)8 ≈ 19.234 in decimal. Again, we have rounded up 7 x 8-2 = 0.109375.
Decimal to any base conversion
We can convert a decimal number to its equivalent in any base. We need two procedures, one for the integral part and one for the fractional part.
We call the integral part of the decimal number the source and the integral part of the converted number the destination. We first create an empty destination. We then repetitively divide the source to get the quotient and the remainder. The remainder is inserted to the left of the destination. The quotient becomes a new source. Figure 2.7 shows how the destination is made with each repetition.We use Figure 2.6 to illustrate the process manually with some examples.
Example 2.11
The following shows how to convert 35 in decimal to binary. We start with the number in decimal, we move to the left while continuously finding the quotients and the remainder of division by 2. The result is 35 = (100011)2:
Example 2.12
The following shows how to convert 126 in decimal to its equivalent in the octal system. We move to the right while continuously finding the quotients and the remainder of division by 8. The result is 126 = (176)8:
Example 2.13
The following shows how we convert 126 in decimal to its equivalent in the hexadecimal system. We move to the right while continuously finding the quotients and the remainder of division by 16. The result is 126 = (7E)16:
Converting the fractional part
The fractional part can be converted using repetitive multiplication. We call the fractional part of the decimal number the source and the fractional part of the converted number the destination. We first create an empty destination. We then repetitively multiply the source to get the result. The integral part of the result is inserted to the right of the destination, while the fractional part becomes the new source. Figure 2.8 shows the UML diagram for the process. Figure 2.9 shows how the destination is made in each repetition. We use the figure to illustrate the process manually with some examples.
Example 2.14
Convert the decimal number 0.625 to binary.
Solution
Since the number 0.625 has no integral part, the example shows how the fractional part is calculated. The base here is 2. Write the decimal number at the left corner. Multiply the number continuously by 2 and record the integral and fractional part of the result. The fractional part moves to the right, and the integral part is recorded under each operation. Stop when the fractional part is 0 or there are enough bits. The result is (0.101)2:
Example 2.15
The following shows how to convert 0.634 to octal using a maximum of four digits. The result is 0.634 = (0.5044)8. Note that we multiply by 8 (base octal):
Example 2.16
The following shows how to convert 178.6 in decimal to hexadecimal using only one digit to the right of the decimal point. The result is 178.6 = (B2.9)16 Note that we divide or multiply by 16 (base hexadecimal):
Example 2.17
An alternative method for converting a small decimal integer (usually less than 256) to binary is to break the number as the sum of numbers that are equivalent to the binary place values:
Using this table, we can convert 165 to binary (10100101)2 as shown below:
Example 2.18
A similar method can be used to convert a decimal fraction to binary when the denominator is a power of two:
Using this table, we convert 27⁄64 to binary (0.011011)2 as shown below:
Align these fractions according to decimal equivalent values. Note that since 1⁄2 and 1⁄16 are missing, we replace them with 0s:
Number of digits
We often need to know the number of digits before converting a number from decimal to other bases. In a positional number system with base b, we can always find the number of digits of an integer using the relation K = ⎡logb N⎤, in which ⎡x⎤ means the smallest integer greater than or equal to x (it is also called the ceiling of x), and N is the decimal value of the integer. For example, we can find the required number of bits in the decimal number 234 in all four systems as follows:
a. In decimal: Kd = ⎡log10 234⎤ = ⎡2.37⎤ = 3, which is obvious.
b. In binary: Kb = ⎡log2 234⎤ = ⎡7.8⎤ = 8. This is true because 234 = (11101010)2
c. In octal: Ko = ⎡log8 234⎤ = ⎡2.62⎤ = 3. This is true because 234 = (352)8
d. In hexadecimal: Kh = ⎡log16 234⎤ = ⎡1.96⎤ = 2. This is true because 234 = (EA)16
Binary–hexadecimal conversion
We can easily change a number from binary to hexadecimal and vice versa. The reason is that there is a relationship between the two bases: four bits in binary is one digit in hexadecimal. Figure 2.10 shows how this conversion can be done.
Example 2.19
Show the hexadecimal equivalent of the binary number (10011100010)2.
We first arrange the binary number in 4-bit patterns start from far right: 100 1110 0010. Note that the leftmost pattern can have one to four bits. We then use the equivalent of each pattern shown in Table 2.2 in section 2.2.5 to change the number to hexadecimal: (4E2)16.
Example 2.20
What is the binary equivalent of (24C)16?
Each hexadecimal digit is converted to 4-bit patterns: 2 → 0010, 4 → 0100, and C → 1100. The result is (001001001100)2.
Binary–octal conversion
We can easily convert a number from binary to octal and vice versa. The reason is that there is an interesting relationship between the two bases: three bits is one octal digit. Figure 2.11 shows how this conversion can be done.
Example 2.21
Show the octal equivalent of the binary number (101110010)2.
Each group of three bits is translated into one octal digit. The equivalent of each 3‑bit group is shown in Table 2.2 in section 2.2.5. The result is (562)8.
Example 2.22
What is the binary equivalent of for (24)8?
Write each octal digit as its equivalent bit pattern to get (010100)2.
Octal–hexadecimal conversion
It is not difficult to convert a number in octal to hexadecimal or vice versa. We can use the binary system as the intermediate system. Figure 2.12 shows an example.
The following illustrates the process:
❑ To convert from octal to hexadecimal, we first convert the number in the octal system to binary. We then rearrange the bits in groups of four bits to find the hexadecimal equivalent.
❑ To convert from hexadecimal to octal, we first convert the number in the hexadecimal system to binary. We then rearrange the bits in groups of three to find the octal equivalent.
Number of digits
In conversion from one base to another, we often need to know the minimum number of digits we need in the destination system if we know the maximum number of digits in the source system. For example, if we know that we use at most six decimal digits in the source system, we want to know the minimum number of binary digits we need in the destination system.
In general, assume that we are using K digits in base b1 system. The maximum number we can represent in the source system is b1K – 1. The maximum number we can have in the destination system is b2x – 1. Therefore, b2x – 1 ≥ b1K – 1. This means b2x ≥ b1K, which means:
Example 2.23
Find the minimum number of binary digits required to store decimal integers with a maximum of six digits.
K = 6, b1 = 10, and b2 = 2. Then x = ⎡K x (log b1 / log b2)⎤ = ⎡6 x (1 / 0.30103)⎤ 5 20. The largest six-digit decimal number is 999 999 and the largest 20-bit binary number is 1 048 575. Note that the largest number that can be represented by a 19-bit number is 524287, which is smaller than 999 999. We definitely need 20 bits.
2.3 NONPOSITIONAL NUMBER SYSTEMS
Although non-positional number systems are not used in computers, we give a short review here for comparison with positional number systems. A non-positional number system still uses a limited number of symbols in which each symbol has a value. However, the position a symbol occupies in the number normally bears no relation to its value—the value of each symbol is fixed. To find the value of a number, we add the value of all symbols present in the representation. In this system, a number is represented as:
and has the value of:There are some exceptions to the addition rule we just mentioned, as shown in Example 2.24.
Example 2.24
The Roman number system is a good example of a non-positional number system. This system was invented by the Romans and was used until the sixteenth century in Europe. Roman numerals are still used in sports events, clock dials, and other applications. This number system has a set of symbols S = {I, V, X, L, C, D, M}. The values of each symbol are shown in Table 2.3.
To find the value of a number, we need to add the value of symbols subject to specific rules:
- When a symbol with a smaller value is placed after a symbol having an equal or larger value, the values are added.
- When a symbol with a smaller value is placed before a symbol having a larger value, the smaller value is subtracted from the larger one.
- A symbol S1 cannot come before another symbol S2 if S1 ≤ 10 x S2. For example, I or V cannot come before C.
- For large numbers a bar is placed above any of the six symbols (all symbols except I) to express multiplication by 1000. For example, V = 5 000 and M = 1 000 000.
- Although Romans used the word nulla (nothing) to convey the concept of zero, the Roman numerals lack a zero digit in their system.
The following shows some Roman numbers and their values: