# The Number Systems

## The Number Systems

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 RDBMS connection

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

Table L-1: Number Systems

System

Elements

Decimal

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

Binary

{0,1}

{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).

### Converting numbers

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

#### Binary to decimal conversion

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 nth (leftmost) element, calculate the sum of each element times 2 powered by n:

(An * 2) n + (An-1 * 2) n-1 + ... + (A0 * 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) 4 (2 * 0) 3 (2 * 0) 2 (2 * 1) 1 (2 * 1) 0 Result 16 0 0 2 1 Subtotal 16+0+0+2+1=19

#### Decimal to binary conversion

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 nj to N
WHILE nj <> 0
{
aj := remainder of nj / 2
nj + 1 := floor (nj / 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 nj 123 61 30 15 7 3 1 aj 61 30 15 7 3 1 0 Remainder 1 1 0 1 1 1 1

 BackCover
 SQL Bible
 Preface
 Part I: SQL Basic Concepts and Principles
 Part II: Creating and Modifying Database Objects
 Part III: Data Manipulation and Transaction Control
 Part IV: Retrieving and Transforming Data
 Part V: Implementing Security Using System Catalogs
 Part VI: Beyond SQL--Procedural Programming and Database Access Mechanisms
 List of Figures
 List of Tables
 List of Code Examples
 List of Sidebars
 CD Content