Cs271 Chapter 04 Numerical Systems
Cs271 Chapter 04 Numerical Systems
Data Representation
2
External Representation
Binary Number System
◦ has 2 digits: 0 and 1 (binary digit)
◦ place values determined by powers of 2.
◦ can uniquely represent any integer value in theory
3
Binary Representation
Place values (right-to-left) are 20, 21, 22, 23, 24, etc.
Bits are numbered (right-to-left) starting at 0.
Place value depends on number of "bits" defined.
Example:
◦ A 16-bit integer is transcribed as 0000 0000 1011 0010
(assuming red is "on")
4
Converting binary to decimal
5
Converting decimal to binary
Example: 157 => 0b10011101
6
Converting decimal to binary
Example: 157 => 0b10011101
0b1 _ _ _ _ _ _ _
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
7
Converting decimal to binary
Example: 157 => 0b10011101
0b1 _ _ 1 _ _ _ _
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
8
Converting decimal to binary
Example: 157 => 0b10011101
0b1 _ _ 1 1 _ _ _
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
9
Converting decimal to binary
Example: 157 => 0b10011101
0b1 _ _ 1 1 1 _ _
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
10
Converting decimal to binary
Example: 157 => 0b10011101
0b1 _ _ 1 1 1 _ 1
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
11
Converting decimal to binary
Example: 157 => 0b10011101
0b1 0 0 1 1 1 0 1
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
12
Converting decimal to binary
Example: 157 => 0b10011101
0b1 0 0 1 1 1 0 1
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
13
Converting decimal to binary
Example: 157 => 0b10011101
2 157
0b1 0 0 1 1 1 0 1 0 ..R 1
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
14
Hexadecimal Representation
The hex number system has 16 digits:
◦ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Place values (right-to-left) are ...
◦ 160, 161, 162, 163, 164, etc.
Hexadecimal is commonly used for easily
converting binary to a more manageable
form.
◦ 16 = 24, thus 4 binary digits can be represented
as one hex digit.
15
Practice
Convert 6077(decimal) into Hex number.
◦ First, convert into 16-bit Binary
◦ Then transcribe to Hex
16
Representing negative integers
Must specify size!
◦ Specify n: number of bits (8, 16, 32, etc.)
◦ There are 2n possible "codes“
0b _ _ _ _ _ _ _ _ _
M L
S S
B B 18
Binary form of negative numbers
Several methods, each with disadvantages.
We will focus on two’s-complement form
◦ Signed magnitude representation (SMR)
◦ One’s complement
◦ Two’s complement
19
Signed number representations
Decimal Binary Negative SMR One’s Two’s
0 0000 -0 1000 1111 0000
1 0001 -1 1001 1110 1111
2 0010 -2 1010 1101 1110
3 0011 -3 1011 1100 1101
4 0100 -4 1100 1011 1100
5 0101 -5 1101 1010 1011
6 0110 -6 1110 1001 1010
7 0111 -7 1111 1000 1001
8 1000 -8 - - 1000
20
Binary form of negative numbers
Example: -13 in 16-bit twos-complement
◦ |-13| = 13 = 0000 0000 0000 1101
◦ ones-complement is 1111 1111 1111 0010
◦ add 1 to get 1111 1111 1111 0011 = -13
Hexadecimal representation?
◦ Convert binary to hex in the usual way
◦ -13 = 1111 1111 1111 0011 = FFF3h = 0xfff3
24
Character and control codes
Letters, digits, special characters … are represented
internally as numbers
ASCII 256 codes (1-byte)
◦ e.g., 'A' … 'Z' are codes 65 - 90
◦ e.g., '0' … '9' are codes 48 - 57
Unicode 65,536 codes (2-byte)
Some codes are used for controlling devices
◦ e.g., code 10 is “new line” for output device
◦ e.g., code 27 is Esc (“escape” key)
Device controllers translate codes (device-dependent)
All keyboard input is character (including digits)
25
ASCII Codes
26
Digits
Digits entered from the keyboard are characters
◦ E.G., ‘0’ is character number 48, … ‘9’ is character number 57
27
Neutral representation
Inside the computer
◦ Bytes, words, etc., can represent a finite number of combinations
of off/on switches.
◦ Each distinct combination is called a code.
◦ Each code can be used to represent:
numeric value
memory address
machine instruction
keyboard character
other characters
Representation is neutral.
◦ The operating system and the programs decide how to interpret
the codes.
28
Interpreting codes
It is especially important to learn to interpret
hexadecimal (external representation) codes.
◦ Frequently used by assembly and debugging systems
29
Arithmetic operations
The following examples use 8-bit two’s-complement
operands
◦ Everything extends to 16-bit, 32-bit, n-bit representations.
◦ What is the range of values for 8-bit operands?
Signed: -128 to 127
Unsigned: 0 to 255
30
Binary Addition
Specify result size (bits)
Binary addition table: + 0 1
0 0 1
1 1 10
Example: 00101101
- 00011110
00001111
32
Verification
Perform operation on binary operands
Convert result to decimal
Convert operands to decimal
Perform operation on decimal operands
[Convert result to binary]
Compare results
33
Binary Multiplication
Usual algorithm (45 × 5 )
00101101
× 101
00101101
001011010
11100001
34
Binary Multiplication
Usual algorithm (45 × 5 ) Repeated addition (45 + 45 +… )
00101101 00101101
× 101 + 00101101
00101101 + 00101101
001011010 + 00101101
11100001 + 00101101
11100001
35
Binary Multiplication
Usual algorithm (45 × 5 ) Repeated addition (45 + 45 +… )
00101101 00101101
× 101 + 00101101
00101101 + 00101101
001011010 + 00101101
11100001 + 00101101
11100001
… or shift left (and add leftovers, if multiplier is not a
power of 2) (e.g., 45 x 5 = 45 × 4 + 45 )
0010110100 (00101101<<2) is 45 × 4
+ 00101101
1110001
11100001
110 : Quotient
111 101101
111
1000
111
11 : Remainder
37
Binary Division
Usual algorithm (45 ÷ 7 ) Repeated subtraction
◦ Count … until remainder is less than
divisor
00101101
110 : Quotient
- 111
111 101101 - 111
111 #count = Quotient
1000 - 111
…
111
11 : Remainder 11 : Remainder
38
Binary Division
Usual algorithm (45 ÷ 7 ) Repeated subtraction
◦ Count … until remainder is less than
divisor
00101101
110 : Quotient
- 111
111 101101 - 111
111 #count = Quotient
1000 - 111
…
111
11 : Remainder 11 : Remainder
Addition: add
Subtraction: complement and add
Multiplication: repeated add
Division: repeated subtract
Comparison: non-destructive subtract
40
Byte-ordering
When it takes more than one byte to represent a value
Big-endian
◦ Bytes are ordered left -> right (most significant to least
significant) in each word
◦ Used in Motorola architectures (Mac) and others
Little-endian
◦ Bytes are ordered least significant to most significant in each
word
◦ Used in Intel architectures (e.g., IA-32)
For both schemes:
◦ Within each byte, bit values are stored left -> right (as usual)
◦ Each character is one byte
◦ Strings are stored in byte order.
Problem: communicating between architectures.
41
Big Endian and Little Endian
42
Byte-ordering (big-endian)
Example 32-bit integer: -1234
11111111 11111111 11111011 00101110
FF FF FB 2E
Byte3 Byte2 Byte1 Byte0
43
Byte-ordering (little-endian)
Example 32-bit integer: -1234
11111111 11111111 11111011 00101110
FF FF FB 2E
Byte3 Byte2 Byte1 Byte0
44
Communication
Internet communication must be consistent across
architectures
45
Floating-point values
“decimal” means “base ten”
46
Converting floating-point
(decimal - binary) - Subtraction
25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5
32 16 8 4 2 1 0.5 0.25 0.125 0.0625 0.03125
47
Converting floating-point
(decimal - binary) - Subtraction
25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5
32 16 8 4 2 1 0.5 0.25 0.125 0.0625 0.03125
1
48
Converting floating-point
(decimal - binary) - Subtraction
25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5
32 16 8 4 2 1 0.5 0.25 0.125 0.0625 0.03125
1 1
49
Converting floating-point
(decimal - binary) - Subtraction
25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5
32 16 8 4 2 1 0.5 0.25 0.125 0.0625 0.03125
1 1 1
50
Converting floating-point
(decimal - binary) - Subtraction
25 24 23 22 21 20 2-1 2-2 2-3 2-4 2-5
32 16 8 4 2 1 0.5 0.25 0.125 0.0625 0.03125
0 0 0 1 1 0 0 1 0 0 0
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 52
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 53
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 54
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 55
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 56
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 57
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 58
Converting floating-point
(decimal - binary) – Div/Mult
.8125 x 2 = 1.625
.625 x 2 = 1.25
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom) 59
Converting floating-point
(decimal - binary)
61
IEEE 754 standard
Most use a completely different representation (IEEE
standard) and a separate ALU (Floating-Point unit)
3 Levels of precision
◦ Single (32-bit), Double (64-bit), and Extended (80-bit)
3 parts code structure
0 0……0 0……………0
Sign bit
Normalized mantissa
Biased exponent
Three-part code structure
(Single precision: 32-bit)
0 0……0 0……………0
0 0……0 0……………0
0 0……0 0……………0
0 0……0 0……………0
68
Example: Decimal 6.25 to
IEEE754 Single Precision
Find sign bit: Positive number = 0
Convert to binary: 6.25 =>110.01
◦ Method: Successive division by 2 for integer part,
multiplication by 2 for floating part.
6÷2=3R0
3÷2=1R1
1÷2=0R1 (Stop when integer part is 0,
Write remainders, bottom to top)
.25 x 2 = 0.5
.5 x 2 = 1.0 (Stop when fractional part is 0,
Write integers, top to bottom)
69
Example: Decimal 6.25 to
IEEE754 Single Precision
Find sign bit: Positive number = 0
Convert to binary: 110.01
Normalize the binary number:
◦ +1.1001 x 22
70
Example: Decimal 6.25 to
IEEE754 Single Precision
Find sign bit: Positive number = 0
Convert to binary: 110.01
Normalize the binary number: +1.1001 x 22
Calculate biased exponent: Exponent + Bias
◦ Exponent = 2
◦ Bias = 127 in single precision
71
Example: Decimal 6.25 to
IEEE754 Single Precision
Find sign bit: Positive number = 0
Convert to binary: 110.01
Normalize the binary number: +1.1001 x 22
Calculate biased exponent = Exponent + Bias = 129
◦ Exponent = 2
◦ Bias = 127 in single precision
Convert the biased exponent to binary
◦ 129 (in decimal) = 1000 0001 (in binary)
72
Example: Decimal 6.25 to
IEEE754 Single Precision
Find sign bit: Positive number = 0
Convert to binary: 110.01
Normalize the binary number: +1.1001 x 22
Calculate biased exponent: Exponent + Bias = 129
◦ Exponent = 2
◦ Bias = 127 in single precision
Convert the biased exponent to binary
◦ 129 (in decimal) = 1000 0001 (in binary)
Find the normalized mantissa:
◦ 1001
73
Example: Decimal 6.25 to
IEEE754 Single Precision
Find sign bit: Positive number = 0
Convert to binary: 110.01
Normalize the binary number: +1.1001 x 22
Calculate biased exponent: Exponent + Bias = 129
◦ Exponent = 2
◦ Bias = 127 in single precision
Convert the biased exponent to binary
◦ 129 (in decimal) = 1000 0001 (in binary)
Find the normalized mantissa: 1001
Fill the code bits:
0 1000 0001 1001 0000 0000 0000 0000 000
1-bit 8-bit 23-bit
Sign Biased exponent Normalized mantissa 74
32-bit Example: 6.25
6.25 in IEEE single precision
6.25 (decimal) = 110.01 (binary)
Move the radix point until a single 1 appears on the left,
and multiply by the corresponding power of 2
= 1.1001 x 22
… the sign bit is 0 (positive)
… the “biased” exponent is 2 + 127 = 129 = 10000001
… and the “normalized” mantissa is 1001 (drop the 1, and
zero-fill).
Rearrange 0 10000001 10010000000000000000000
0100 0000 1100 1000 0000 0000 0000 0000
= 0x40C80000
75
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
77
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign:
78
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent:
79
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal:
80
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal: 100 0001 1 => 1000 0011 => 128 + 2 + 1 = 131
Subtract Bias:
81
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal: 100 0001 1 => 1000 0011 => 128 + 2 + 1 = 131
Subtract Bias: 131 – 127 = 4
◦ Normalized Mantissa:
82
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal: 100 0001 1 => 1000 0011 => 128 + 2 + 1 = 131
Subtract Bias: 131 – 127 = 4
◦ Normalized Mantissa: 000 0111 0000 0000 0000 0000
Form the normalized binary number:
-1.0000111 x 24
83
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal: 100 0001 1 => 1000 0011 => 128 + 2 + 1 = 131
Subtract Bias: 131 – 127 = 4
◦ Normalized Mantissa: 000 0111 0000 0000 0000 0000
Form the normalized binary number: - 1.0000111 x 24
Unnormalized binary number:
84
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal: 100 0001 1 => 1000 0011 => 128 + 2 + 1 = 131
Subtract Bias: 131 – 127 = 4
◦ Normalized Mantissa: 000 0111 0000 0000 0000 0000
Form the normalized binary number: - 1.0000111 x 24
Unnormalized binary number: -10000.111
Convert to decimal:
85
IEEE754 Single Precision to
Decimal
Number: 0xC1870000
Find binary:
◦ 1100 0001 1000 0111 0000 0000 0000 0000
Find components
◦ 1-bit Sign: 1
◦ 8-bit Biased Exponent: 100 0001 1
Biased Exponent = Exponent + Bias
Convert to decimal: 100 0001 1 => 1000 0011 => 128 + 2 + 1 = 131
Subtract Bias: 131 – 127 = 4
◦ Normalized Mantissa: 000 0111 0000 0000 0000 0000
Form the normalized binary number: - 1.0000111 x 24
Unnormalized binary number: -10000.111
Convert to decimal: -(24 + 0.5 + 0.25 + 0.125)= -16.875
86
32-bit Example: 0xC1870000
What decimal floating-point number is represented by
0xC1870000?
1100 0001 1000 0111 0000 0000 0000 0000
1 10000011 00001110000000000000000
… the sign is negative
… the “unbiased” exponent is 131 - 127 = 4
… and the “unnormalized” mantissa is
1.00001110000000000000000
(insert the 1 left of the radix point).
Move the radix point 4 places to the right: 10000.111
-10000.111 = -16.875
87
Practice
Show the IEEE Standard 754 single-precision binary (32-bit)
representation of the floating-point number -23.625.
◦ Normalized Binary:
◦ Sign:
◦ Biased Exponent:
◦ Normalized Mantissa:
88
Practice II
Convert the number in IEEE representation below to
decimal.
◦ Number:
0 10000000101 1110100110000000000000000000000000000000000000000000
◦ Sign: Positive = 0
◦ Biased Exponent: = 100 0000 0101 (11-bit)
Convert to decimal: 100 0000 0101 => 1 + 22 + 210 = 1029
Exponent = Biased exponent – bias = 1029 - 1023 = 6
◦ Unnormalized Mantissa:
Normalized Mantissa = 1.111010 011 x 26
1111010.011
◦ Decimal number: 122.375
Integer: 11110 => 2 + 23 + 24+ 25 + 26 = 122
Fractional: 0.011 => 0.25 + 0.125 = 0.375
90
Questions?
92