CP1200 – Introduction to Computer Science 1
Decimal, Binary and Hexadecimal Numbers Nerd Test If you laugh at this you must be a nerd ☺ Q: Why do computer nerds mix Halloween up with Christmas? A: OCT 31 = DEC 25
The Decimal Number System – Base 10 The decimal numbering system uses 10 symbols from 0 to 9. 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
The Hexadecimal Number System – Base 16 Where the decimal number system uses 10 symbols (0 to 9), hexadecimal (Hexadecem is Latin for 16) there are 16 symbols. To provide an additional 6 symbols, the letters A to F (or a to f) are used as illustrated below.
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
Hex Digits from 00 to FF Where 2 digits for decimal will span from 00 to 99 providing 100 values, two digits for hexadecimal spans from 00 to FF providing 256 values The complete range of hexadecimal values from 00 to FF 0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F Copyright © Colin Lemmon 2004 – 2006, JCU
1/4
50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF
Q: If you and DEAD people can read HEX how many people can read HEX? A: 57006 (#DEAE)
Binary Numbers "There are 10 kinds of people in the world. Those that understand binary, and those that don’t" Binary is used because it can be represented by on/off, true/false, 1/0 and so on making it simple to implement (in hardware). Binary Decimal Hexadecimal 0000
00
0
0001
01
1
0010
02
2
0011
03
3
0100
04
4
0101
05
5
0110
06
6
0111
07
7
1000
08
8
1001
09
9
1010
10
A
1011
11
B
1100
12
C
1101
13
D
1110
14
E
1111
15
F
Copyright © Colin Lemmon 2004 – 2006, JCU
2/4
Binary to Decimal Conversion 10011010 The digits range from lowest order on the right, to highest on the left (like decimal and hex). The decimal value can be calculated by raising 2 to the power of the position or column in which the digit resides. 27 26 25 24 23 22 21 20 Because each value is raised to the power of 2, the value of the column is simply double the value of the next column. The columns above therefore evaluate to the decimal values below. 128 64 32 16 8 4 2 1 Converting the binary number to decimal simply requires the value in the column to be multiplied by the value of the column. As the value in a column can only be 0 or 1, this is very simple. 128 1
64 0
32 0
16 1
8 1
4 0
2 1
1 0
128 + 0 + 0 + 16 + 8 + 0 + 2 + 0 = 154 So the decimal equivalent of 10011010 is 154.
Decimal to Binary Conversion Decimal to binary conversion requires that we move from the highest value column (left) to the lowest value column (right) and check whether we need a 1 in that column (if the decimal value of the column fits into the number remaining), and if so, we subtract the value of the column from the value, continuing until the whole number is represented. For example convert the value 78 to binary: 78 128s 0 78 64s 1 78 – 64 = 14 (14 left to represent) 32s 0 14 16s 0 14 8s 1 14 – 8 = 6 4s 1 6–4=2 2s 1 2–2=0 (finished with 1s, but we must fill every column to the right) 1s 0 0 So, the binary equivalent of 78 is 01001110.
Copyright © Colin Lemmon 2004 – 2006, JCU
3/4
Converting Between Binary and Hex Converting Binary to hex and hex to binary is quite simple, as each hex digit (0-15) is the same as 4 binary digits: 0000 to 1111 is also 0 to 15. Converting binary to hex requires that the binary number is broken up into blocks of 4 digits and the corresponding hex digit of 0 – F is substituted from the table below. Example, convert the binary value 0101010 to hex: 0010 2
1010 A
Converting from hex to binary is done in the same way. For example convert the hex value B6 to binary. B 1011
6 0110 Binary Hexadecimal 0000
0
0001
1
0010
2
0011
3
0100
4
0101
5
0110
6
0111
7
1000
8
1001
9
1010
A
1011
B
1100
C
1101
D
1110
E
1111
F
Using Hex Numbers in Code In C/C++, hex numbers are preceded by the sequence 0x (zero and x). Note that the digits A to F can be upper or lower case. int value = 0xB6; is the same as int value = 182; Copyright © Colin Lemmon 2004 – 2006, JCU
4/4