You are on page 1of 8

CHAPTER 3 Number Representations 1.

Counting Decimal
0
•  Integer 1 •  Base 10
2 •  Digits 0..9 (=10-1)
–  Decimal, Binary, Hexadecimal, General Radix 3
4 •  Keep incrementing rightmost digit
–  Unsigned vs. Signed 5
until highest digit is reached, then
6
•  Real 7 apply same mechanism to
8
9 increment digit to the left und
–  Fixed Point 10 reset current digit to 0.
11
–  Floating Point …
19
20
..
99
100

Bräunl 2014 1 Bräunl 2014 2

Counting Binary Counting Hexadecimal


0 0
0 0 •  Base 2 1 1 •  Base 16
1 1 2 2
10 2 •  Digits 0..1 (=2-1) 3 3 •  Digits 0..9, A..F (15=16-1)
11 3 4 4
100 4 •  Keep incrementing rightmost digit 5 5 •  Keep incrementing rightmost digit
6 6
101 5 until highest digit is reached, then 7 7 until highest digit is reached, then
110 6 apply same mechanism to 8 8 apply same mechanism to
111 7 9 9
1000 8 increment digit to the left und A 10 increment digit to the left und
reset current digit to 0. B 11 reset current digit to 0.
C 12
D 13
E 14
F 15
10 16
11 17
… …
1F 31
20 32
Bräunl 2014 3 Bräunl 2014 4
Counting, General Case Radix R Gottfried Wilhelm von Leibniz
•  Base R •  Developed binary number system
0 0 •  Digits 0..R-1 •  Build mechanical calculators
1 1 based on this principle (1690)
2 2 •  Keep incrementing rightmost digit
… … until highest digit is reached, then •  Also discovered infinitesimal
R-1 R-1
R apply same mechanism to calculus, topology, theory of
10
11 R+1 increment digit to the left und motion (physics), and many more
… …
reset current digit to 0. •  “Last Universal Genius”

Leibniz, 1646 – 1716


Philosopher, Alchemist,
Mathematician (Germany)

Bräunl 2014 5 Bräunl 2014 Source: Wikipedia 6

2. Polynomial Representation Polynomial Representation


•  Positional Number System •  Positional Number System
•  Each digit is weighted according to its position •  Each digit is weighted according to its position
•  Example for integer decimal (base 10): •  General Case Radix R
741210 = 7*103 + 4*102 +1*101 +2*100 dn dn-1 … d0 R
•  Example for integer binary (base 2):
10102 = 1*23 + 0*22 +1*21 + 0*20 •  Decimal equivalent:
= 810+210 = 1010 = dn*Rn + dn-1*Rn-1 + … + d0*R0
•  Example for integer hex (base 16):
1A516 = 1*162 + 10*161 + 5*160
= 25610 + 16010 + 510 = 42110
Bräunl 2014 7 Bräunl 2014 8
Integer Conversion Decimal ! Binary Conversion
•  Radix R ! Decimal Convert: 4510
We have just done •  45 /2 = 22 Remainder 1
•  22 /2 = 11 Remainder 0
•  Decimal ! Radix •  11 /2 = 5 Remainder 1
while (number != 0) •  5 /2 = 2 Remainder 1
{ digit = number % R; // remainder = new digit (right to left) •  2 /2 = 1 Remainder 0
number = number / R; // integer division •  1 /2 = 0 Remainder 1
} finished

Result: 1011012
Bräunl 2014 9 Bräunl 2014 10

Decimal ! Hex Conversion Binary ↔ Hex Conversion


Convert: 4510 Four binary digits always make up one hex digit:
•  45 /16 = 2 Remainder D
•  2 /16 = 0 Remainder 2
finished

Result: 2D16

Bräunl 2014 11 Bräunl 2014 12


3. Fixed Point Real Numbers Fixed Point Real Numbers
•  Each digit is weighted according to its position •  Each digit is weighted according to its position
•  General Case Radix R •  Example for real decimal (base 10):
dn dn-1 … d0 . d-1 d-2 … R 74.1210 = 7*101 + 4*100 + 1*10-1 + 2*10-2
•  Decimal equivalent: •  Example for real binary (base 2):
= dn*Rn + dn-1*Rn-1 +…+ d0*R0 + d-1*R-1 + d-2*R-2 +… 10.102 = 1*21 + 0*20 +1*2-1 + 0*2-2
= 210 + 0.510 = 2.510
•  Example for real hex (base 16):
1.A516 = 1*160 + 10*16-1 + 5*16-2
= 110 + 0.62510 + 0.0195312510
= 1.6445..10 (= 42110 /162)
Bräunl 2014 13 Bräunl 2014 14

Fixed Point Conversion Decimal ! Binary Conversion


•  Radix R ! Decimal Convert: 0.4510 with max. 6 decimals
We have just done •  0.45 *2 = 0.9 Integer part 0
•  Decimal ! Radix •  0.9 *2 = 1.8 Integer part 1
•  Convert integer part as before •  0.8 *2 = 1.6 Integer part 1
•  Use algorithm for fractional part:
•  0.6 *2 = 1.2 Integer part 1
while (fraction != 0 && count <= precision)
•  0.2 *2 = 0.4 Integer part 0
{ fraction = fraction * R;
•  0.4 *2 = 0.8 Integer part 0
digit = trunc(fraction); // integer part = new digit (left to ri.)
fraction = fraction – digit; // new fractional part stop (max. precision reached)
count ++; Result: 0.0111002 (= 0.437510)
}
Bräunl 2014 15 Bräunl 2014 16
Decimal ! Hex Conversion 4. Signed Numbers
Convert: 0.4510 with max. 3 decimals •  Signed numbers result
010 R naturally from counting
•  0.45 *16 = 7.2 Integer part 7 0 0 R-1 R-1
backwards
… …
•  0.2 *16 = 3.2 Integer part 3 0 0 2 positive 2
•  Number representations
001 1
•  0.2 *16 = 3.2 Integer part 3 000 0 have a certain precision
R-1 R-1 R-1 -1 and therefore a fixed
stop (max. precision reached) R-1 R-1 R-2 negative -2
number of digits
… …
Result: 0.73316 (= 0.44995.. 10) •  Example: General case
Radix R, 3 digits

Bräunl 2014 17 Bräunl 2014 18

Signed Binary Numbers Signed Binary Numbers


0111 7 •  Counting backwards •  Can be
0110 6 1111 = -1 0000 = 0
0101 5 •  Example: Binary, 4 digits viewed as
0100 4 1110 = -2 0001 = 1 a circle
0011 3
0010 2 •  Note: 1101 = -3 0010 = 2
0001 positive 1 negative number range =
0000 0
1111 -1 positive number range +1 1100 = -4 negative positive 0011 = 3
1110 negative-2
1101 -3 1011 = -5 0100 = 4
1100 -4 •  Note:
1011 -5 Highest bit indicates 1010 = -6 0101 = 5
1010 -6
1001 -7
negative number
1001 = -7 0110 = 6
1000 -8
1000 = -8 0111 = 7
Bräunl 2014 19 Bräunl 2014 20
Signed Hex Numbers Negating Binary Numbers
7F 127 •  Counting backwards •  Negation: Example: binary 4 bits:
7E 126
.. .. •  Example: Hex, 2 digits Convert a positive number 510 = 01012s
10 16 to a negative 1.  NOT (flip all bits):
0F 15 or vice versa
.. .. •  Note: 0101 ! 1010
01 positive 1 negative number range = 2.  Increment
00 0 •  Algorithm for negating
positive number range +1 1010 ! 1011
FF -1 binary numbers:
FE negative-2
.. .. 2’s Complement: Result 10112s = -510
F0 -16
EF -17 1.  NOT (aka 1 s complement)
.. .. 2.  Increment
81 -127
80 -128
Bräunl 2014 21 Bräunl 2014 22

Negating Binary Numbers Signed Numbers


Example, binary 4 bits: Apply same algorithm Note:
510 = 01012s to neg. number: •  Depending on context (signed or unsigned), the same
1.  NOT (flip all bits): 10112s = -510 bit pattern can mean different things.
0101 ! 1010 1.  NOT (flip all bits): e.g.: 10112 signed = -5
2.  Increment 1011 ! 0100 10112 unsigned = +11
1010 ! 1011 2.  Increment
0100 ! 0101 •  Negation by 2 s complement can be built very simple in
Result 10112s = -510 hardware. This allows to use a standard hardware adder
Result 01012s = +510 for subtraction as well.

Bräunl 2014 23 Bräunl 2014 24


Signed Numbers Signed Fixed-Point Numbers
Note: Combine both concepts – see examples:
•  Correct transition between negative and positive range •  1111.102s = -TwoCom (1111.102)
(or vice versa) is called a roll-over. = -(0000.012 + 0.012)
e.g.: 11112s + 00012s = 00002s = - 0000.102
-110 + 110 = 010 = - 0.510
•  Incorrect transition between negative and positive range •  0111.102s = +0111.102 = +7.510
(or vice versa) is called an overflow.
e.g.: 01112s + 00012s ≠ 10002s •  1101.012s = -TwoCom(1101.012)
+710 + 110 ≠ -810 = -(0010.102 + 0.012)
= -0010.112
= -2.7510
Bräunl 2014 25 Bräunl 2014 26

5. Floating Point Numbers Floating Point


•  We use the IEEE standard for FP numbers •  Def.: Number10 = (-1)sign * 2(exponent-127) * 1.fraction2
•  32 bits, single precision: •  Example:
1 bit sign, 8 bits exponent, 23 bits fraction
0 10000000 00000000000000000000000

•  Calculate back:
Result = + 2128-127 * 1. 0000..2
•  Def.: Number10 = (-1)sign * 2(exponent-127) * 1.fraction2 = 2 * 12
= 210
•  Example = + 2-3 * 1.0100..2 = 0.001012 •  Exponent is simply a shifting of the "decimal point"
= 0.1562510
Bräunl 2014 Source: Wikipedia 27 Bräunl 2014 28
Floating Point Floating Point Examples
•  Def.: Number10 = (-1)sign * 2(exponent-127) * 1.fraction2 •  Def.: Number10 = (-1)sign * 2(exponent-127) * 1.fraction2
•  Example: •  Examples:
1 01111110 10000000000000000000000 2.0010 ! 0 10000000 00000000000000000000000
1.0010 ! 0 01111111 00000000000000000000000
•  Calculate back: 0.7510 ! 0 01111110 10000000000000000000000
Result = - 2126-127 * 1. 1000..2 0.5010 ! 0 01111110 00000000000000000000000
= - 2-1 * 1.12 -0.5010 ! 1 01111110 00000000000000000000000
= - 0.112 -0.7510 ! 1 01111110 10000000000000000000000
= - 0.7510 0.0010 ! 0 00000000 00000000000000000000000

Bräunl 2014 29 Bräunl 2014 30

Special Cases
Exponent = 0
•  Fraction = 0 ! result = 0
•  Fraction ≠ 0 ! result = (-1)sign * 2(-126) * 0.fraction2
"denormal number"

Exponent = 1..254
•  Regular case ! result = (-1)sign * 2(exp-127) * 1.fraction2

Exponent = 255
•  Fraction = 0 ! result is ± infinity
•  Fraction ≠ 0 ! result is NaN ("not a number")
Bräunl 2014
after invalid operation, e.g. /0 31

You might also like