## Number Systems in Computer

### Number Systems

Human beings use *decimal* (base 10) and *duodecimal* (base 12) number systems for counting and measurements (probably because we have 10 fingers and two big toes). Computers use *binary* (base 2) number system, as they are made from binary digital components (known as transistors) operating in two states - on and off. In computing, we also use *hexadecimal* (base 16) or *octal* (base 8) number systems, as a *compact* form for represent binary numbers.

#### Decimal Number System (Base 10):

Decimal number system has ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, called *digit*s. It is a *positional notation*, for example,

735D = 7×10^{2}+ 3×10^{1}+ 5×10^{0}

We shall denote a decimal number with an optional suffix `D`

if ambiguity arises.

#### Binary Number System (Base 2):

Binary number system has two symbols: 0 and 1, called *bits*. It is also a *positional notation*, for example,

10110B = 1×2^{4}+ 0×2^{3}+ 1×2^{2}+ 1×2^{1}+ 0×2^{0}

We shall denote a binary number with a suffix `B`

. Some programming languages denote binary numbers with prefix `0b`

(e.g., `0b1001000`

), or prefix `b`

with the bits quoted (e.g., `b'10001111'`

).

A binary digit is called a *bit*. Eight bits is called a *byte* (why 8-bit unit? Probably because `8=2`

).^{3}

#### Hexadecimal Number System (Base 16):

Hexadecimal number system uses 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, called *hex digits*. It is a *positional notation*, for example,

A3EH = 10×16^{2}+ 3×16^{1}+ 14×16^{0}

We shall denote a hexadecimal number with a suffix `H`

. Some computer languages denote hex numbers with prefix `0x`

(e.g., `0x1A3C5F`

), or prefix `x`

with hex digit quoted (e.g., `x'C3A4D98B'`

).

Each hexadecimal digit is also called a *hex digit*. Most languages accept lowercase `'a'`

to `'f'`

as well as uppercase `'A'`

to `'F'`

.

Computers uses binary system in their internal operations, as it is built from binary digital electronic components. However, writing or reading a long sequence of binary bits is cumbersome and error-prone. Hexadecimal system is used as a *compact* form or *shorthand* for binary bits. Each hex digit is equivalent to 4 binary bits, i.e., shorthand for 4 bits, as follows:

0H (0000B) (0D) | 1H (0001B) (1D) | 2H (0010B) (2D) | 3H (0011B) (3D) |

4H (0100B) (4D) | 5H (0101B) (5D) | 6H (0110B) (6D) | 7H (0111B) (7D) |

8H (1000B) (8D) | 9H (1001B) (9D) | AH (1010B) (10D) | BH (1011B) (11D) |

CH (1100B) (12D) | DH (1101B) (13D) | EH (1110B) (14D) | FH (1111B) (15D) |

### Number Systems Conversion

#### Conversion from Hexadecimal to Binary:

Replace each hex digit by the 4 equivalent bits, for examples,

A3C5H = 1010 0011 1100 0101B 102AH = 0001 0000 0010 1010B

#### Conversion from Binary to Hexadecimal:

Starting from the right (least significant bit), replace each group of 4 bits by the equivalent hex digit, for examples,

1001001010B = 0010 0100 1010B = 24AH 10001011001011B = 0010 0010 1100 1011B = 22CBH

It is important to note that hexadecimal number provides a *compact form* or *shorthand* for representing binary bits.

#### Conversion from Base *r* to Decimal (Base 10):

Given a *n*-digit base *r* number: `dn-1 dn-2 dn-3 ... d3 d2 d1 d0`

(base r), the decimal equivalent is given by:

dn-1 × r^{(}n-1) + dn-2 × r^{(}n-2) + ... + d1 × r^{1}+ d0 × r^{0}

#### Conversion from Decimal (Base 10) to Base *r*:

Use repeated division/remainder. For example,

To convert 261D to hexadecimal: 261/16 quotient=16 remainder=5 16/16 quotient=1 remainder=0 1/16 quotient=0 remainder=1 (quotient=0 stop) Hence, 261D = 105H

**Hint**: You could use the Windows' *Calculator* to carry out number conversion by choosing the scientific mode (Launch "Calculator" ⇒ Select "View" menu ⇒ Choose "Scientific" mode).

### Exercises

- Convert the following
*decimal*numbers into*binary*and*hexadecimal*numbers.108; 4848; 9000

- Convert the following binary numbers into hexadecimal and decimal numbers.
1000011000; 10000000; 101010101010

- Convert the following hexadecimal numbers into binary and decimal numbers.
ABCDE; 1234; 80F

You could use the Windows' Calculator (`calc.exe`

) to check your answer, by setting it to the scientific mode (Select "View" menu ⇒ choose "Scientific" mode).

**Answers:**

`1101100B`

,`1001011110000B`

,`10001100101000B`

,`6CH`

,`12F0H`

,`2328H`

;`218H`

,`80H`

,`AAAH`

,`536D`

,`128D`

,`2730D`

;`10101011110011011110B`

,`1001000110100B`

,`100000001111B`

,`703710D`

,`4660D`

,`2063D`

.

### References & Resources

- N/A

#### Latest Post

- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync