The decimal number system we casually use in our everyday lives is not the only number system in the world. Most of us are used to the decimal system, take it for granted, and maybe even consider it the only possible collection of numbers. But that assumption is valid only if we are talking about modern humans. Machines don't use the decimal system. Moreover, humans themselves, now and in the past, have used other systems. For example, the ancient civilization of Sumerians used a base-sixty number system six thousand years ago; that is, they had 60 different characters to represent digits. In our own time, the English word "dozen" points to the existence of a numbering system different from decimal.

The bases relevant to computer science in general and to SQL in particular are represented in Table L-1.

System |
Elements |
---|---|

Decimal |
{0,1,2,3,4,5,6,7,8,9} |

Binary |
{0,1} |

Hexadecimal |
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} |

The hexadecimal system is useful to represent large binary numbers. For example, a 16-bit binary sequence, called a *word* in programming jargon, can be divided into four groups with four bits in each group, and then each group can be easily represented as a hexadecimal number (Table L-2) that is more convenient to use.

Binary number |
0010111011111100 |
|||

Binary groups |
0010 |
1110 |
1111 |
1100 |

Hexadecimal groups |
2 |
E |
F |
C |

Note |
Since the largest possible four-bit number (1111) is a decimal 15, you cannot represent it (as well as numbers 10,11, 12, 13, and 14) using just one character in the decimal number system. The hexadecimal system is very convenient in this case because 1111 can be represented as hexadecimal F (the equivalent to decimal 15). |

Numbers can be converted from one number system to another using a mathe-matical algorithm — a sequence of operations.

A binary number could be converted into decimal using the following algorithm: Count the elements comprising the number (zeroes and ones) from right to left using 0 for the first element, 1 for the second, and so on until the last element *n*. Then, starting from the *n*th (leftmost) element, calculate the sum of each element times 2 powered by *n*:

(

A* 2)_{n}^{n}+ (A* 2)_{n-1}+ ... + (^{n-1}A_{0}* 2)^{0}

Table L-3 illustrates the conversion of binary number 10011 to its decimal equivalent.

Binary Number |
1 |
0 |
0 |
1 |
1 |

n |
4 |
3 |
2 |
1 |
0 |

Calculation |
(2 * 1) |
(2 * 0) |
(2 * 0) |
(2 * 1) |
(2 * 1) |

Result |
16 |
0 |
0 |
2 |
1 |

Subtotal |
16+0+0+2+1=19 |

A decimal number can be converted into binary using this logic: Divide a decimal number by 2 using integer division. Write down the remainder (from right to left). Repeat the operation using the resulting number. Repeat until the resulting number becomes zero:

SET j to 0 SET n_{j}to N WHILE n_{j}<> 0 { a_{j}:= remainder of n_{j}/ 2 n_{j}_{+ 1}:= floor (n_{j}/ 2) j := j + 1 }

Table L-4 illustrates the conversion of decimal number 123 to its binary equivalent. The resulting binary number is 1111011.

N = 123 |
123 | ||||||

J |
0 |
1 |
2 |
3 |
4 |
5 |
6 |

n |
123 |
61 |
30 |
15 |
7 |
3 |
1 |

a |
61 |
30 |
15 |
7 |
3 |
1 |
0 |

Remainder |
1 |
1 |
0 |
1 |
1 |
1 |
1 |