Computer Architecture
CS F342
CA-LECT7
Arithmetic for computers
BITS Pilani, Hyderabad Campus
Arithmetic for computers
To address issues like:
Fractions and other real numbers
If operation creates a number bigger
than we can represent
How does hardware really multiply or
divide numbers
BITS Pilani, Hyderabad Campus
Binary representation
The binary number
01011000 00010101 00101110 11100111
Most significant bit Least significant bit
represents the quantity
0 x 231 + 1 x 230 + 0 x 229 + + 1 x 20
A 32-bit word can represent 232 numbers between
0 and 232-1
this is known as the unsigned representation as
were assuming that numbers are always positive
BITS Pilani, Hyderabad Campus
ASCII vs binary
Does it make more sense to represent a decimal number
in ASCII?
Hardware to implement arithmetic would be difficult
What are the storage needs? How many bits does it
take to represent the decimal number 1,000,000,000 in
ASCII and in binary?
In binary: 30 bits (230 > 1 billion)
In ASCII: 10 characters, 8 bits per char = 80 bits
BITS Pilani, Hyderabad Campus
Signed / unsigned
The hardware recognizes two formats:
unsigned (corresponding to the C declaration unsigned int)
-- all numbers are positive, a 1 in the most significant bit
just means it is a really large number
signed (C declaration is signed int or just int)
-- numbers can be +/- , a 1 in the MSB means the number
is negative
This distinction enables us to represent twice as many
numbers when were sure that we dont need negatives
BITS Pilani, Hyderabad Campus
Floating Point numbers
Normalized scientific notation: single non-zero digit to the
left of the decimal (binary) point example: 3.5 x 109
1.010001 x 2-5two = (1 + 0 x 2-1 + 1 x 2-2 + + 1 x 2-6) x 2-5ten
A standard notation enables easy exchange of data between
machines and simplifies hardware algorithms the
IEEE 754 standard defines how floating point numbers
are represented
BITS Pilani, Hyderabad Campus
Sign and Magnitude Representation
Sign Exponent Fraction
1 bit 8 bits 23 bits
S E F
More exponent bits wider range of numbers (not necessarily more
numbers recall there are infinite real numbers)
More fraction bits higher precision
Register value = (-1)S x F x 2E
Since we are only representing normalized numbers, we are
guaranteed that the number is of the form 1.xxxx..
Hence, in IEEE 754 standard, the 1 is implicit
Register value = (-1)S x (1 + F) x 2E
BITS Pilani, Hyderabad Campus
Sign and Magnitude Representation
Sign Exponent Fraction
1 bit 8 bits 23 bits
S E F
Largest number that can be represented: 2.0 x 2128 = 2.0 x 1038
Smallest number that can be represented: 2.0 x 2-128 = 2.0 x 10-38
Overflow: when representing a number larger than the one above;
Underflow: when representing a number smaller than the one above
Double precision format: occupies two 32-bit registers:
Largest: Smallest:
Sign Exponent Fraction
1 bit 11 bits 52 bits
S E F
BITS Pilani, Hyderabad Campus
Exponent representation
To simplify sort, sign was placed as the first bit
For a similar reason, the representation of the exponent is also
modified: in order to use integer compares, it would be preferable to
have the smallest exponent as 000 and the largest exponent as 111
This is the biased notation, where a bias is subtracted from the
exponent field to yield the true exponent
IEEE 754 single-precision uses a bias of 127 (since the exponent
must have values between -127 and 128)double precision uses
a bias of 1023
Final representation: (-1)S x (1 + Fraction) x 2(Exponent Bias)
BITS Pilani, Hyderabad Campus
Examples
Final representation: (-1)S x (1 + Fraction) x 2(Exponent Bias)
Represent -0.75ten in single and double-precision formats
Single: (1 + 8 + 23)
1 0111 1110 1000000
Double: (1 + 11 + 52)
1 0111 1111 110 1000000
What decimal number is represented by the following
single-precision number?
1 1000 0001 010000000
-5.0
BITS Pilani, Hyderabad Campus