Appendix C. Numbering Systems: Binary, Decimal, and Hexadecimal

There are three numbering systems commonly used in computer programming: binary, decimal, and hexadecimal. The binary numbering system is called Base-2 because it has only two digits: 0 and 1. The decimal system is called Base-10 and is the one with which you are most familiar as it is used in everyday life. The hexadecimal system is called Base-16 and is comprised of the numerals 0-9 and the letters A-F to represent values from 10 to 15. Computers use the binary system exclusively in the hardware, but to make programming easier, compilers support decimal and hexadecimal (and the little-used Octal numbering system—Base-8).

Binary

Binary numbers use the Base-2 system where the numbers are represented by digits of either 0 or 1. This is the system the computer uses to store all the data in memory. Each digit in the number represents a power of two.

Binary System

Position

Digit

1

0

2

1

The best way to read a binary number is right to left, as the first digit is to the far right and the last digit is to the far left. The number 1101, read from right to left, has the following order: 1, 0, 1, 1. The position of each digit determines the value of that digit, and each position is twice as large as the previous (with the first digit representing 0 or 1). Here is a breakdown:

Binary Values Table

Position

Value

Position

Value

1

1

17

65,536

2

2

18

131,072

3

4

19

262,144

4

8

20

524,288

5

16

21

1,048,576

6

32

22

2,097,152

7

64

23

4,194,304

8

128

24

8,388,608

9

256

25

16,777,216

10

512

26

33,554,432

11

1,024

27

67,108,864

12

2,048

28

134,217,728

13

4,096

29

268,435,456

14

8,192

30

536,870,912

15

16,384

31

1,073,741,824

16

32,768

32

2,147,483,648

Using this table you can decode any binary number as long as you remember to read the number from right to left and add up each value. How about an example?

The number 10101110 can be decoded as:

0 * 1 = 0
1 * 2 = 2
1 * 4 = 4
1 * 8 = 8
0 * 16 = 0
1 * 32 = 32
0 * 64 = 0
1 * 128 = 128

Adding up the values 2 + 4 + 8 + 32 + 128 = 174. Anyone can read a binary number in this way, so long as one reads from right to left. With a little practice you will be converting binary numbers in your head in only a few seconds.

Decimal

You have probably been using the decimal system since childhood and don't even think about counting numbers in specific digits, as you have been practicing for so long. The Base-10 numbering system is a very natural way for humans to count since we have 10 fingers. But from a scientific point of view, it's possible to decode a decimal number by adding up its digits, as we did for binary.

For example, let's decode the number 247. What makes this number “two hundred forty seven?” The decimal system has 10 digits (thus the name decimal) that go from 0 to 9. Just as with the binary system, you decode the number from right to left (although it is read from left to right in normal use). Because each digit in 247 represents a value to the power of 10, you can decode it as:

7 * 1 = 7

4 * 10 = 40

2 * 100 = 200

Adding up the values 7 + 40 + 200 = 247. Now this is asinine for the average person, but for a programmer, this is a good example for understanding the other numbering systems and is a good lesson.

Hexadecimal

The hexadecimal system is a Base-16 numbering system that uses the numbers 0 to 9 and the letters A to F (to represent the numbers 10 to 15, since each position must be represented by a single digit). Decoding a hexadecimal number works exactly the same as it does for binary and decimal, from right to left, by adding up the values of each digit. For reference, here is a breakdown of the values in the hexadecimal system:

Hexadecimal Table

Value

Digit

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

10

A

11

B

12

C

13

D

14

E

15

F

To read a hexadecimal number (in other words, to convert it to decimal so a human can understand it), just decode the hexadecimal digits from right to left using the table of values and multiply each digit by a successive power of 16. While it was easy to calculate Base-2 multipliers, it is a little more difficult with hexadecimal. Since “hex” numbers go up in value so quickly, there are usually very few digits in a hex number (just look at the huge number after only just 10 digits). Here is a table of multipliers for Base-16:

Hexadecimal Table

Position

Multiplier

0

1

1

16 (16^1)

2

16 (16^2)

3

256 (16^3)

4

4096 (16^4)

5

65,536 (16^5)

6

1,048,576 (16^6)

7

16,777,216 (16^7)

8

268,435,456 (16^8)

9

4,294,967,296 (16^9)

10

68,719,476,736 (16^10)

With this newfound information you should be able to decode any hex number. For instance, the hex number 9C56D is decoded like so:

D: 1 * 13 = 13

6: 16 * 6 = 96

5: 256 * 5 = 1,280

C: 4,096 * 12 = 49,152

9: 65,536 * 9 = 589,824

Adding these values up results in 13 + 96 + 1,280 + 49,152 + 589,824 = 640,365. Since these numbers grow so quickly in Base-16, they are usually grouped in twos and fours where humans will need to read them. Any hex number beyond four digits is usually too much for the average programmer to calculate in his/her head. However, the small size of a hex number usually means it cuts out several digits from a decimal number, which makes for more efficient storage in a file system. For this reason, hex numbers are used in compression and cryptography.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.226