Professional Documents
Culture Documents
7 CS1FC16 Machine Arithmetic
7 CS1FC16 Machine Arithmetic
2's complement
2's complement is biased – there is one more negative number than positive
Example: using 3 bits: can represent 8 numbers:
000 = 0
001 = 1
010 = 2
011 = 3
100 = -4
101 = -3
110 = -2
111 = -1
Complement of –4 is –4: 011 + 1 = 100 – a negative number that stays negative after you negate it –
weird number fault.
Some language standards require that this bug is present in the language – for uniformity.
Wraparound
011 + 001 = 111
3 + 1 = -4
If the largest possible number is increased, it continues from the smallest possible value.
Almost every computer suffers wraparound.
Floating point
Floating point numbers are made of mantissa, exponent and sign bit:
A = (-1)S x 2E x M
There are bit patterns reserved for -0, -∞, +∞ and NaNs.
Zero – not directly representable in the straight format, due to the assumption of leading 1 in
front of the mantissa. Zero is therefore denoted with an exponent field of all 0s and mantissa
field of all 0s. The sign bit is 0 for +0 and 1 for –0.
0 and –0 are distinct values, but they both compare as equal.
Infinity – the values of -∞ and +∞ are denoted with an exponent of all 1s and mantissa of all
0s. The sign bit distinguishes between positive and negative infinity.
Not a Number – NaN is a value that doesn't represent a real number. All NaNs are represented
by a bit pattern with an exponent of all 1s and non-zero mantissa. There are 2 categories of
NaNs – QNaN (quiet NaN) and SNaN (signaling NaN).
o QNaN – a NaN with the most significant mantissa bit 1. They are generated from
operations when the result is not mathematically defined.
o SNaN - a NaN with the most significant mantissa bit 0. They are used to signal and
exception in an operation.
IEEE arithmetic has 2m+1 – 2 NaNs - with m being the number of bits in mantissa:
2m different mantissas x 2(sign bit 1 or 0) - 2(+/- ∞).