COMPX203 Computer Systems: Number Representation
COMPX203 Computer Systems: Number Representation
Number Representation
How Many Are There?
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
+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
• 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
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
• Several others
• e.g. NaN
IEEE-754 Range
• 2-126 to 2127
~ 10-38 to 1038
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 1.xxxx 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)