COMPX203 Computer Systems
Number Representation
How Many Are There?
11? 10112? 138? B16? 10.625?
01000001001010100000000000000000?!
Numbers - Outline
• Number systems
• Integer representations
• Floating point
• Examples
Introduction
• In the computer, everything is binary.
• Therefore, we need ways to map all data into
binary numbers, and vice versa.
Introduction
• Format used will depend on data…
• e.g. consider the number 9
• 9 may be represented as 01001
• -9 may be represented as 10111
• 9.0 may be represented as 0000100100000000
• the character 9 may be represented as 0111001
• …and the intended purpose of the number within
the computer.
Binary Representation (Recap)
• Each bit represents a value which is twice that of
the bit to its right
Dec Bin
0 000 = 0x22 + 0x21 + 0x20
1 001 = 0x22 + 0x21 + 1x20
2 010 = 0x22 + 1x21 + 0x20
3 011 = 0x22 + 1x21 + 1x20
4 100 = 1x22 + 0x21 + 0x20
Other Bases (Recap)
• Computers can only store and manipulate binary
numbers, so binary is necessary for us to use.
• However, large binary numbers are tedious for
humans to process.
• Converting between binary and decimal is also a
tedious process.
• Other bases (which are themselves a power of 2,
such as 8 and 16) convert easily to binary, so are
often used by programmers.
• Hexadecimal, Octal
Hexadecimal Representation (Recap)
0 0000
• Each Hex number can be perfectly 1 0001
represented by 4 binary digits 2 0010
• Two Hex digits can be used to represent a 3 0011
byte; four for a word, etc. 4 0100
5 0101
0010 1110 1010 0001 6 0110
= 2EA116 7 0111
8 1000
9 1001
• With Octal, each digit perfectly represents A
B
1010
1011
3 binary digits C 1100
• Useful for concepts such as ‘rwx’ on the file D 1101
system for Read, Write, eXecute E 1110
F 1111
Integer Formats using Binary
• Sign and Magnitude
• Excess Notation
• One’s complement
• Two’s complement
Considerations
• When a coding format is being devised, a
number of considerations need to be
made:
• Ease of manipulation
• Conciseness and machine efficiency
• Accuracy sufficient for problem
• Standardised data communication
Sign?
• Negative integers are often required
• Computers do not have internal minus signs
• There are several ways to represent negative and
positive integers
• Choice is often based on the ease of manipulation
for the intended purpose
Sign and Magnitude
• One of the simplest systems is to allocate one bit as
the “sign” bit. The other bits are the “magnitude”.
• E.g. +24 = 0001 1000
-24 = 1001 1000
• Advantage:
• Simple to find the sign.
• Disadvantage:
• Two values for zero (“positive” and “negative” zero)
• CPU must process the sign separately.
Excess Notation Bit String Value
0000 -8
0001 -7
• Smallest number with 1 in the 0010 -6
Most Significant Bit (MSB) 0011 -5
represents zero (e.g. 1000) 0100 -4 neg
• All bit strings greater than this 0101 -3
0110 -2
represent +ve numbers. 0111 -1
• All bit strings less than this 1000 0
represent -ve numbers. 1001 1
1010 2
• Example is known as excess eight 1011 3
notation because 8 (1000) 1100 4 pos
represents zero. 1101 5
1110 6
1111 7
One’s Complement
• Negative numbers formed by inverting the positive
representation (bitwise NOT):
• e.g. +24 = 00011000
-24 = 11100111
• Two values for zero (all ‘1s’, and all ‘0s’)
• Addition requires carry bit to be wrapped around.
Two’s Complement
• To negate a number, invert the bits (bitwise NOT)
and then add one.
• E.g. +24 = 00011000
Invert: = 11100111
Add one: = 11100111 + 1
-24 = 11101000
• Works for converting negative to positive as well.
Two’s Complement
• Can represent numbers in Binary
the range -2n-1 to Negative
(2n-1 - 1)
Complement
• Only one value for zero 0 1
1 0
Positive
+1
Result
Two’s Complement – Number Wheel
• Four bits:
-1 +0
-2 1111 0000 +1
1110 0001
-3 +2
1101 0010
-4 1100 0011 +3
-5 1011 0100 +4
1010 0101
-6 +5
1001 0110
-7 1000 0111 +6
-8 +7
Reference: Katz: Contemporary
Logic Design, p243
Two’s Complement
• Leftmost bit only ever set when value is negative.
• All zeroes is the only representation of zero.
• Simple to implement negation, addition and
subtraction in hardware.
• Can be sign extended.
• For example, 8-bit to 16-bit:
• +510 = 00000101 = 0000000000000101
• -510 = 11111011 = 1111111111111011
Integer Representation – Summary
• One’s complement common in older computer
hardware.
• Two’s complement most common signed integer
representation today.
• Sign and magnitude used in some very early
computers.
• Sign and magnitude and Excess notation both used
in common floating point formats (up next!)
Floating Point Numbers
IEEE-754
Floating Point Numbers
• Number representations considered so far (i.e. signed and
unsigned integers) have a limited range dependent on the
number of bits:
• 16 bits 0 to 65535 for unsigned
-32768 to 32767 for 2's complement
• 32 bits 0 to 4294967295 for unsigned
-2147483648 to 2147483647 2's complement
• How do we represent the following numbers:
• Mass of an electron =
0.00000000000000000000000000000910956 kg
• Mass of the earth = 5975000000000000000000000 kg
• Both these numbers have few significant digits but are well
beyond the range above.
Scientific Notation
• We normally use scientific notation for such
numbers:
• Mass of electron = 9.10956 x 10-31 kg
• Mass of earth = 5.975 x 10+24 kg
• These numbers can be split into two components:
• Mantissa
• Exponent
• e.g. 9.10956 x 10-31 kg
• mantissa = 9.10956
• exponent = -31
• radix, or base = 10
Defining a Floating Point Number
• Several things must be defined, in order to define a
floating point number:
• Size of mantissa e.g. 9.10956
• Sign of mantissa e.g. Positive
• Size of exponent e.g. 31
• Sign of exponent e.g. Negative
• Number base in use e.g. 10
We are trading off precision and possibly making computation times
longer, in exchange for being able to represent a larger range of
numbers.
IEEE-754 Floating Point Numbers
• Uses 32 bits to represent a number:
• 1 sign bit
• 8 bits for exponent
• Remaining 23 bits for mantissa
• In order to represent negative exponents, use
excess-128 notation.
• Range of numbers represented is approximately:
• 10-38 to 10+38 (in decimal terms)
IEEE-754 Floating Point Numbers
• The precision of the mantissa can be improved
from 23 bits to 24 bits by noticing that the MSB of
the mantissa in a normalized binary number is
always “1”
• And so, it can be implied. The added complications
(see below) are felt to be a good trade-off for the
added precision.
• This complication is the fact that certain numbers
are too small to be normalized, i.e. the number 0.0
cannot be represented at all!
IEEE-754 Floating Point Numbers
• IEEE Standard 754
• Most computer manufactures use IEEE-754 format.
• Number represented:
(-1)S * (1.M)*2(E - Bias)
2 main formats: single and double.
1 8 bits 23 bits
Single S Exponent Mantissa Bias = 127
1 11 bits 52 bits
Double S Exponent Mantissa Bias = 1023
IEEE-754 Special Cases
Exponent Mantissa
• Zero, represented by 0 0
• +/- infinity 255 Not 0
• Several others
• e.g. NaN
IEEE-754 Range
• 2-126 to 2127
~ 10-38 to 1038
• Similarly, the double-precision used 64 bits, and
represents a range of approximately 10-300 to 10300
IEEE-754 Examples
1 8 23
Single S Exponent Mantissa Bias = 127
(-1)S * (1.M)*2(E - Bias)
Example:What Decimal number does the following
single precision IEEE floating point number represent?
0 10000001 10110000000000000000000
Sign = 0 (positive)
Exponent = 0x81 or 12910 ⇒ 𝐸 − 𝐵𝑖𝑎𝑠 = 2
Mantissa = 1 .10110000000000000000000
implied
IEEE-754 Examples
(-1)S * (1.M)*2(E - Bias)
0 10000001 10110000000000000000000
==
(-1)0 * (1.1011)*2(2)
110.112 or 6.7510
Bin
0.1 = 0x2-1 = 1/2 = 0.5
0.01 = 0x2-2 = 1/4 = 0.25
0.001 = 0x2-3 = 1/8 = 0.125
More IEEE-754 Examples
• What is the decimal number -32.125 converted to
IEEE floating point format?
• First, convert the number to binary (without sign):
100000.001
• Then, normalize for [Link] format:
1.00000001 × 25 (shift = 5)
• Add in the exponent bias (127) and convert the total
(132) to binary (10000100 or 0x84)
• Assemble final number (with sign bit):
1 10000100 00000001000000000000000
IEEE-754 Implementation
• Floating point arithmetic may be implemented in
software or hardware.
• It is much more complex than integer arithmetic.
• Hardware instruction implementations require more
logic gates than equivalent integer instructions, and are
often slower.
• Software implementations are generally very slow.
• CPU floating point performance (flops) can be very
different from integer performance.
Numbers – Summary
• Different formats to represent:
• Positive and Negative values
• Very large and very small values
• IEEE-754 is commonly used to represent floating
point numbers
• 32-bit (float)
• 64-bit (double)