You are on page 1of 29

Number Systems

and
Number
Representation

1
Goals of this Lecture

Help you learn (or refresh your memory) about:


• The binary, hexadecimal, and octal number systems
• Finite representation of unsigned integers
• Finite representation of signed integers

Why?
• A power programmer must know number systems and data
representation to fully understand C++’s primitive data
types

Primitive values and


the operations on them
2
The Decimal Number System
Name
• “decem” (Latin) => ten

Characteristics
• Ten symbols
•0 1 2 3
4 5 6 7
8 9
• Positional
• 2945 ≠
(Most) 2495
people use the decimal number system
Why?
• 2945 =
(2*103)
+
(9*102)
+
(4*101) 3
+
The Binary Number System
Name
• “binarius” (Latin) => two

Characteristics
• Two symbols
•0 1
• Positional
• 1010B ≠
1100B

Most (digital)
computers use
the binary
number
system
4
W
Decimal-Binary Equivalence
Decimal Binary Decimal Binary
0 0 16 10000
1 1 17 10001
2 10 18 10010
3 11 19 10011
4 100 20 10100
5 101 21 10101
6 110 22 10110
7 111 23 10111
8 1000 24 11000
9 1001 25 11001
10 1010 26 11010
11 1011 27 11011
12 1100 28 11100
13 1101 29 11101
14 1110 30 11110
15 1111 31 11111
... ...
5
The Octal Number System
Name
• “octo” (Latin) => eight

Characteristics
• Eight symbols
•0 1 2 3 4 5 6 7
• Positional
• 1743O ≠ 7314O

Computer programmers often use the octal number system


Why?

6
Decimal-Octal Equivalence
Decimal Octal Decimal Octal Decimal Octal
0 0 16 20 32 40
1 1 17 21 33 41
2 2 18 22 34 42
3 3 19 23 35 43
4 4 20 24 36 44
5 5 21 25 37 45
6 6 22 26 38 46
7 7 23 27 39 47
8 10 24 30 40 50
9 11 25 31 41 51
10 12 26 32 42 52
11 13 27 33 43 53
12 14 28 34 44 54
13 15 29 35 45 55
14 16 30 36 46 56
15 17 31 37 47 57
... ...
7
The Hexadecimal Number System
Name
• “hexa” (Greek) => six
• “decem” (Latin) => ten

Characteristics
• Sixteen symbols
•0 1 2 3 4 5 6 7 8 9 A B C D E F
• Positional
• A13DH ≠ 3DA1H

Computer programmers often use the hexadecimal number


system
Why?

8
Decimal-Hexadecimal Equivalence
Decimal Hex Decimal Hex Decimal Hex
0 0 16 10 32 20
1 1 17 11 33 21
2 2 18 12 34 22
3 3 19 13 35 23
4 4 20 14 36 24
5 5 21 15 37 25
6 6 22 16 38 26
7 7 23 17 39 27
8 8 24 18 40 28
9 9 25 19 41 29
10 A 26 1A 42 2A
11 B 27 1B 43 2B
12 C 28 1C 44 2C
13 D 29 1D 45 2D
14 E 30 1E 46 2E
15 F 31 1F 47 2F
... ...
9
Conversions from one
Base to another Base
Decimal-Binary Conversion
Decimal to binary
• Repeatedly divide by 2, consider
remainder
37 / 2 = 18 R 1
18 / 2 = 9 R 0
9 / 2 = 4 R 1 Read from bottom
4 / 2 = 2 R 0 to top: 100101B
2 / 2 = 1 R 0
1 / 2 = 0 R 1

11
Decimal-Octal Conversion
Decimal to octal:

31 / 8 = 3 R 7 Read from bottom


3 / 8 = 0 R 3 to top: 37O

Octal to decimal: expand using positional notation

37O = (3*81) + (7*80)


= 24 + 7
= 31

14
Decimal-Hexadecimal Conversion
Hexadecimal to decimal: expand using positional notation
25H = (2*161) + (5*160)
= 32 5
+
= 37

Decimal to hexadecimal: use the shortcut

37 / 16 = 2 R 5 Read from bottom


2 / 16 = 0 R 2 to top: 25H

16
Binary-Octal Conversion
Observation: 81 = 23
• Every 1 octal digit corresponds to 3 binary digits

Binary to octal
Digit count in binary number
001010000100111101B
1 2 0 4 7 5O not a multiple of 3 =>
pad with zeros on left
Octal to binary

1 2 0 4
7 5O
001010000100111101B

18
Binary-Hexadecimal Conversion
Observation: 161 = 24
• Every 1 hexadecimal digit corresponds to 4 binary digits

Binary to hexadecimal
Digit count in binary number
1010000100111101B
A 1 3 DH not a multiple of 4 =>
pad with zeros on left
Hexadecimal to binary

A 1 3 DH Discard leading zeros


1010000100111101B from binary number
if appropriate

Is it clear why programmers


often use hexadecimal?
20
Adding Unsigned Integers
Two more examples
1
3 0011B
+ 10 + 1010B
-- ----
13 1101B

11
7 0111B
+ 10 + 1010B

1 10001B

24
Subtracting Unsigned Integers
One more example
12
0202
10 1010B
- 7 - 0111B

3 0011B

Start at right column


Proceed leftward
Borrow 2 when
necessary

27

You might also like