Module 4 - Number System and IEEE Floating Point Representation
Module 4 - Number System and IEEE Floating Point Representation
Module 4 - Number System and IEEE Floating Point Representation
• Data in Computers
• Binary Arithmetic
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Data in Computers
Data in Computers
• Computers understand only two things – 0 and 1
• Data is stored only the form of combinations of 0s and 1s
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Data in Computers
• Kinds of data we store in computers:
• Integer numbers
• 1, 3, 94, -103, etc. We will study more
• Floating point numbers deeply
We use digits
0 to 9 -> 10 symbols (digits) (the decimal system)
What is so special about the number 10?
Nothing!
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
The numbering system for
computers
• Computers use binary numbering system
• i.e. it has only two symbols to represent numbers – 0 and 1
• Just like humans have 10 symbols - (0 to 9)
• Other systems
• Octal – 8 symbols
• 0, 1, 2, 3, 4, 5, 6, 7
• Hexadecimal – 16 symbols
• 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Decimal vs Binary vs Octal vs
Hexadecimal: An example
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
How a decimal number is
interpreted?
Eg.: 357
Digits -> 3 5 7
Weights-> 10^2 10^1 10^0
MSD LSD
Perform sum over Digits*Weights:
3*10^2 + 5*10^1 + 7*10^0
= 357
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Range of binary numbers
One bit – up to decimal 1
Two bits – up to 3
Three bits – up to 7
In general:
n bits - 2^n – 1 in decimal
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Examples (Worked out on
Board)
• Convert 10101 from binary to decimal
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Negative Binary Numbers
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Negative Binary Numbers
• To handle negative numbers: left-most bit is used to indicate the sign.
• Known by various names: Most Significant Bit (MSB), sign-bit or high-order
bit
• Positive numbers: MSB is 0
• Negative numbers: MSB is 1
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Signed-magnitude
representation
MSB is the sign as usual
7 bits for the magnitude or the value
Range: -127 to 127
Examples:
109 01101101
-109 11101101
127 01111111
-127 11111111
What about zero?
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
1’s complement
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
2’s complement
2’s complement of a no. = it’s 1’s complement + 1
Range: -128 to 127
Example: calculating the two’s complement representation of -15
Decimal 15 00001111
In one’s complement 11110000
Adding 1 +1
In two’s complement 11110001
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Binary Arithmetic
Binary arithmetic rules
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Subtraction using 2 CT
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Subtraction using 2 CT:
Example 1
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Substraction using 2CT:
Example 2
-3
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Addition in 2’s Complement
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Addition in 2’s Complement
➢ Overflows occur when the sum does not fit in the given number of bits
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Converting binary real
numbers to decimal
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Storing Real number in
Computers
Steps to convert a real number in decimal to binary:
1. First, we convert the real number in decimal to a binary real
number.
• E.g. 10.6875 is converted into 1010.1011
2. Then we normalize the binary real number.
• E.g. 1010.1011 can be written as 1.0101011 x 23
3. Encode the normalized binary real number into IEEE 754 32-
bit or 64-bit floating point format
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
IEEE 754 floating point
representation
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
IEEE 754 floating point
representation (contd.)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
IEEE floating point
representation
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Storing 10.6875 int IEEE 754
32-bit Floating Point Format
10.6875 ➔ 1010.1011 ➔ 1.0101011 x 23
binary Normalised binary
s=0
e = (3+127)2=(130)2 = 10000010
m = 01010110000000000000000
which is
0 10000010 01010110000000000000000
IEEE 754 32-bit Floating Point representation
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Conversion to decimal real
number (Worked out on board)
Convert the following number to decimal:
1 00000111 11000000000000000000000
It is
-1.75 x 2 (7-127) = - 1.316554 x 10-36
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
IEEE floating point
representation
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 1
Convert 12.375 into 32-bit IEEE 754 Floating Point Format
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 2
Convert the following binary number in 32-bit IEEE 754 floating
point format into decimal:
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 3 (Work out on board)
Convert -10.7 into 32-bit IEEE 754 Floating Point Format
Store only the bits that can fit your 23-bits mantissa. Rest are
truncated.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus