Module 4 - Number System and IEEE Floating Point Representation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Module 4 – Number System

BITS Pilani Dr. Jagat Sesh Challa


Pilani Campus
Department of Computer Science & Information Systems
Module Overview

• Data in Computers

• Binary Numbering System

• Binary Arithmetic

• IEEE Floating Point Representation

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

• Computers use switches to store 0 or 1


• Stores 1 if switch is ON
• Stores 0 if switch is OFF

• Computers consists of Integrated circuits (IC) of billions of


switches; allow storage of huge amounts of information.

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

• 1.00433, 54.9090354598, etc.


• Characters
• ‘A’, ‘a’, ‘#’, etc.
• Strings
• “Delhi”, “Gopal”, etc.
• etc.
All of them are stored as binary patterns, i.e. strings of 0s and 1s.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Binary Numbering System


The human numbering system

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

• Convert 16 from decimal to binary

• Convert 23 from decimal to binary

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Negative Binary Numbers

• How do we signify negative numbers in arithmetic?

• The – symbol to the left of MSD

• So… 1111 is 15, then -1111 should -15

• But… computers cannot handle any symbol apart from 0


and 1

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

• For 8 bit unsigned numbers: range of 0 to 255 (2^8 – 1)


• What about signed numbers?
o -127 to 127 or -128 to 127

• How does the computer know whether to treat a number as signed or


unsigned?
o It cannot. It’s the programmer’s job to tell.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Negative Binary Numbers
Three schemes for representing negative binary numbers:
• Signed-magnitude representation
• One’s complement representation
• Two’s complement representation

• We will discuss them with respect to 8-bit integers

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

As before, MSB indicates the sign.


Negative no. = One’s complement of the positive number
One’s complement → Invert all the bits → 1s to 0s and 0s to 1s
Range: -127 to 127
Examples:
15 00001111
-15 11110000
85 01010101
-85 10101010
What about zero?

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

What about zero?

Most widely used!!

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Binary Arithmetic
Binary arithmetic rules

0 1 is carry. In binary addition carry is discarded


Note: for a
positive number,
its binary
representation in
SMF, 1CT or 2CT
– it is the same
1 Borrow 1 from next high order digit number itself

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

IEEE Floating Point Representation


Converting real numbers to
binary

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Converting binary real
numbers to decimal

Exercise: Convert this


binary floating-point
number to decimal system

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

Let us study the IEEE 754 Floating


Point Representation

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.)

Single precision (32 bit)

Double precision (64 bit)

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:

1 1011 0110 011 0000 0000 0000 0000 0000

( −1) 1 × 210110110 −01111111 × 1.011


= − 1.375 × 255
= −49539595901075456.0
= − 4.9539595901075456 × 1016

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

You will see a recurring pattern of bits that is never-ending.

What should you do?

Store only the bits that can fit your 23-bits mantissa. Rest are
truncated.

This leads to approximation.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

You might also like