# Appendix B. Number Systems

## B. Number Systems

Here are only numbers ratified.

William Shakespeare

Nature has some sort of arithmetic- geometrical coordinate system, because nature has all kinds of models. What we experience of nature is in models, and all of nature's models are so beautiful.

It struck me that nature's system must be a real beauty, because in chemistry we find that the associations are always in beautiful whole numbersthere are no fractions.

Richard Buckminster Fuller

#### Objectives

In this appendix you will learn:

• To understand basic number systems concepts, such as base, positional value and symbol value.

• To understand how to work with numbers represented in the binary, octal and hexadecimal number systems.

• To abbreviate binary numbers as octal numbers or hexadecimal numbers.

• To convert octal numbers and hexadecimal numbers to binary numbers.

• To convert back and forth between decimal numbers and their binary, octal and hexadecimal equivalents.

• To understand binary arithmetic and how negative binary numbers are represented using two's complement notation.

Outline

B.1 Introduction

B.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers

B.3 Converting Octal and Hexadecimal Numbers to Binary Numbers

B.4 Converting from Binary, Octal or Hexadecimal to Decimal

B.5 Converting from Decimal to Binary, Octal or Hexadecimal

B.6 Negative Binary Numbers: Two's Complement Notation

### B.1. Introduction

In this appendix, we introduce the key number systems that programmers use, especially when they are working on software projects that require close interaction with machine-level hardware. Projects like this include operating systems, computer networking software, compilers, database systems and applications requiring high performance.

When we write an integer such as 227 or 63 in a program, the number is assumed to be in the decimal (base 10) number system. The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The lowest digit is 0 and the highest digit is 9one less than the base of 10. Internally, computers use the binary (base 2) number system. The binary number system has only two digits, namely 0and 1. Its lowest digit is 0 and its highest digit is 1one less than the base of 2.

As you will see, binary numbers tend to be much longer than their decimal equivalents. Programmers who work in assembly languages and in high-level languages that enable programmers to reach down to the machine level, find it cumbersome to work with binary numbers. So two other number systemsthe octal number system (base 8) and the hexadecimal number system (base 16)are popular primarily because they make it convenient to abbreviate binary numbers .

In the octal number system, the digits range from 0 to 7. Because both the binary number system and the octal number system have fewer digits than the decimal number system, their digits are the same as the corresponding digits in decimal.

The hexadecimal number system poses a problem because it requires 16 digitsa lowest digit of 0 and a highest digit with a value equivalent to decimal 15 (one less than the base of 16). By convention, the letters A through F represent the hexadecimal digits corresponding to decimal values 10 through 15. Thus, in hexadecimal, you can have numbers like 876 consisting solely of decimal-like digits, numbers like 8A55F consisting of digits and letters and numbers like FFE consisting solely of letters . Occasionally, a hexadecimal number spells a common word such as FACE or FEEDthis can appear strange to programmers accustomed to working with numbers. The digits of the binary, octal, decimal and hexadecimal number systems are summarized in Figs. B.1 and B.2.

##### Figure B.1. Digits of the binary, octal, decimal and hexadecimal number systems.

Binary digit

Octal digit

Decimal digit

1

1

1

1

2

2

2

3

3

3

4

4

4

5

5

5

6

6

6

7

7

7

8

8

9

9

A (decimalvalue of 10)

B (decimalvalue of 11)

C (decimalvalue of 12)

D (decimalvalue of 13)

E (decimalvalue of 14)

F (decimalvalue of 15)

##### Figure B.2. Comparing the binary, octal, decimal and hexadecimal number systems.

Attribute

Binary

Octal

Decimal

Base

2

8

10

16

Lowest digit

Highest digit

1

7

9

F

Each of these number systems uses positional notationeach position in which a digit is written has a different positional value. For example, in the decimal number 937 (the 9, the 3 and the 7 are referred to as symbol values), we say that the 7 is written in the ones position, the 3 is written in the tens position and the 9 is written in the hundreds position. Note that each of these positions is a power of the base (base 10) and that these powers begin at 0 and increase by 1 as we move left in the number (Fig. B.3).

##### Figure B.3. Positional values in the decimal number system.

Positional values in the decimal number system

Decimal digit

9

3

7

Position name

Hundreds

Tens

Ones

Positional value

100

10

1

Positional value as a power of the base (10)

10 2

10 1

10

For longer decimal numbers, the next positions to the left would be the thousands position (10 to the 3rd power), the ten-thousands position (10 to the 4th power), the hundred-thousands position (10 to the 5th power), the millions position (10 to the 6th power), the ten-millions position (10 to the 7th power) and so on.

In the binary number 101, the right most 1 is written in the ones position, the 0 is written in the twos position and the leftmost 1 is written in the fours position. Each position is a power of the base (base 2) and that these powers begin at 0 and increase by 1 as we move left in the number (Fig. B.4). So, 101 = 1 * 2 2 + 0 * 2 1 + 1 * 2 = 4 + 0 + 1 = 5.

##### Figure B.4. Positional values in the binary number system.

Positional values in the binary number system

Binary digit

1

1

Position name

Fours

Twos

Ones

Positional value

4

2

1

Positional value as a power of the base (2)

2 2

2 1

2

For longer binary numbers, the next positions to the left would be the eights position (2 to the 3rd power), the sixteens position (2 to the 4th power), the thirty-twos position (2 to the 5th power), the sixty-fours position (2 to the 6th power) and so on.

In the octal number 425, we say that the 5 is written in the ones position, the 2 is written in the eights position and the 4 is written in the sixty-fours position. Note that each of these positions is a power of the base (base 8) and that these powers begin at 0 and increase by 1 as we move left in the number (Fig. B.5).

##### Figure B.5. Positional values in the octal number system.

Positional values in the octal number system

Decimal digit

4

2

5

Position name

Sixty-fours

Eights

Ones

Positional value

64

8

1

Positional value as a

8 2

8 1

8

power of the base (8)

For longer octal numbers, the next positions to the left would be the five-hundred-and-twelves position (8 to the 3rd power), the four-thousand-and-ninety-sixes position (8 to the 4th power), the thirty-two-thousand-seven-hundred-and-sixty-eights position (8 to the 5th power) and so on.

In the hexadecimal number 3DA, we say that the A is written in the ones position, the D is written in the sixteens position and the 3 is written in the two-hundred-and-fifty-sixes position. Note that each of these positions is a power of the base (base 16) and that these powers begin at 0 and increase by 1 as we move left in the number (Fig. B.6).

##### Figure B.6. Positional values in the hexadecimal number system.

Positional values in the hexadecimal number system

Decimal digit

3

D

A

Position name

Two-hundred-and-fifty-sixes

Sixteens

Ones

Positional value

256

16

1

Positional value as a power of the base (16)

16 2

16 1

16

For longer hexadecimal numbers, the next positions to the left would be the four-thousand-and-ninety-sixes position (16 to the 3rd power), the sixty-five-thousand-five-hundred-and-thirty-sixes position (16 to the 4th power) and so on.