0% found this document useful (0 votes)
13 views

1.2 Number Representation v12.0

Computers use binary numbers because digital circuits can easily represent two states (ON/OFF) using low and high voltage levels. This requires less circuitry than other number systems like decimal. Binary also avoids issues with differentiating between multiple voltage levels. There are two main methods for representing signed numbers in binary - sign-and-magnitude and two's complement. Two's complement is now more commonly used because it allows for simpler arithmetic operations on signed numbers.

Uploaded by

Amindra De Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

1.2 Number Representation v12.0

Computers use binary numbers because digital circuits can easily represent two states (ON/OFF) using low and high voltage levels. This requires less circuitry than other number systems like decimal. Binary also avoids issues with differentiating between multiple voltage levels. There are two main methods for representing signed numbers in binary - sign-and-magnitude and two's complement. Two's complement is now more commonly used because it allows for simpler arithmetic operations on signed numbers.

Uploaded by

Amindra De Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

Number

Representation in
Computers
Why computers use binary
system?
 Most of data storing,
transferring, and processing in
computers happens with
digital electronics.
 Digital electronics use the
binary system (ON/OFF) or
two different current levels.
For example, 5 volts and 0
volts.
 A signal with a series of ON/OFF pulses is equal to a binary
number.
 This requires the least amount of electronic circuitry systems.
Page 2
Why computers use binary
system?
 If decimal system is used, then 10 different current levels
are required to be used. This requires more circuitry and
additional (10) voltage levels.
 However, least amount of necessary circuitry, will results in
the least amount of space, energy consumption, and cost.
 Further, differentiating between 10 different voltage levels
could be difficult than 2, because of possible fluctuations in
current.
 Moreover, even if binary circuits are combined to represent
decimal, then there will be minimum of 16 levels (2 4 – as
23 is not sufficient), wasting values from 10 to 15.
Page 3
Number System Conversions
 Binary Vs Decimal
 Hexa-Decimal Vs Decimal

Page 4
What is a bit ?
A bit is the most basic unit of information in computing.

It is short for binary digit, which means that it can only


have one of two values, 0 or 1.

1
0

Page 5
Representation of Information
"Word size“ or “Word Length” refers to the multiple
number of bits processed by a computer's CPU in one go
(these days, typically 32 bits or 64 bits).

8 7 6 5 4 3 2 1

An 8-bit word

Page 6
Integers
Integer Number Representations
in the computer

Integer Numbers

Unsigned Numbers Signed Numbers

Sign and Magnitude

Two’s Compliment

Page 8
Un-Signed Numbers
 Unsigned numbers don’t have any sign, these can
contain only magnitude of the number.
 Computers do not allocate space to record the sign of
such numbers.
 So, generally, representation of unsigned binary
numbers are all considered positive numbers only.

Page 9
Un-Signed Number representation

14110 = 100011012

8 7 6 5 4 3 2 1

1 0 0 0 1 1 0 1 = 141

Page 10
Un-Signed Number example

10210 = 11001102

8 7 6 5 4 3 2 1

0 1 1 0 0 1 1 0 = 102

Page 11
Signed Numbers
 In real life we need to be able represent both positive
numbers and negative numbers (signed) numbers
like: -12, -45, +78.
 A signed number MUST have a sign (+ or -). A
method is needed to represent the sign as part of the
binary representation.
 The signed number representation methods are:
 Sign and Magnitude representation
 Two’s-complement representation

Page 12
Sign and Magnitude
Representation
In sign and magnitude (S&M) representation, the leftmost bit of
the word represents the sign of the value:

 0 for positive,
 1 for negative;

The remaining bits represent the numeric value (Magnitude).

Page 13
Sign and Magnitude
Representation
Example 1
Find the S&M representation of -6 10 (in 8-bit word length)

Step 1: Find binary representation using 8 bits


610 = 000001102

Step 2: If the number you want to represent is


negative, change leftmost bit to 1.
10000110

So: -610 = 100001102 (in 8-bit sign/magnitude form)


Page 14
Sign and Magnitude
Representation
Example 2
Find the S&M representation of 7010 (in 8-bit word length)

Step 1: Find binary representation using 8 bits


7010 = 010001102

Step 2: If the number you want to represent is negative, change left


most bit to 1.
01000110 (positive -- no flipping)

So: 7010 = 010001102


(in 8-bit sign/magnitude form)
Page 15
Sign and Magnitude
Representation
Example 3
Find the S&M representation of -3610 (in 8-bit word length)

Step 1: Find binary representation using 8 bits


-3610 = 001001002

Step 2: If the number you want to represent is


negative, change left most bit to 1.
10100100

So: -3610 = 101001002


(in 8-bit sign/magnitude form)
Page 16
Sign and Magnitude
Representation
Exercise
Find the S&M representation of following numbers in both 8-bit and 6 bit word
size.
1. 31
2. -11
3. 27
4. -100

Answers
8-bit 6-bit
31 = 0 0 0 1 1 1 1 1 0 1 1 1 1 1

-11 = 1 0 0 0 1 0 1 1 1 0 1 0 1 1

27 = 0 0 0 1 1 0 1 1 0 1 1 0 1 1

-100 = 1 1 1 0 0 1 0 0 1 0 0 1 0 0
Page 17
Sign and Magnitude
Representation
Exercise
Find the decimal value of following S&M representations
1. 1 1 1 1 1 1 1 1

2. 1 0 0 0 0 0 0 1

3. 0 1 0 1 0 1 0 1

4. 0 0 1 0 1 0 1 0

Answers
1. -127
2. -1
3. 85
4. 42
Page 18
Sign and Magnitude
Representation
Add following values in S&M (using 4 bit word length):

+2 +2
-4 + -2 +

Page 19
Sign and Magnitude
Representation
Disadvantages

(1) Two Representatives for Zero


Using our 4-bit Sign-Magnitude example we have,
+0 = 0000
-0 = 1000

(2). Arithmetic is difficult


Example, lets add Positive 2 to Negative 4, which we Another Example lets add +2 to -2, we expect
expect the answer to be Negative 2. the answer to be zero.

+2 0010 +2 0010
-4 1100 -2 1010
--- ------ --- -----
-6 1110 -4 1100
The result is -6, which is not what we expect The result of -4, which again is not what we
expect

Page 20
Two’s Complement
Representation
 Two’s compliment is another method used to represent binary
numbers by most modern computers.

 The leftmost bit STILL serves as a sign bit:


 0 for positive numbers,
 1 for negative numbers.

 Positive numbers in 2’s compliment are represented just as


same as in Sign and Magnitude.

 However, negative number representation is different.

Page 21
Two’s Complement
Representation
In-order to represent a negative decimal value using Two’s
complement:

1. Temporarily assume the decimal value is positive and convert it to


the binary equivalent and place it in the given word size.

2. Complement (flip each bit, that is if it is 0 make it 1 and visa versa)


the entire positive binary number.

3. Then add one (+1).

Page 22
Two’s Compliment Representation
Represent -7 in 8bit word size.
Step 1: Assume -7 as +7, convert to binary and place it in 8bit word.

8-bit word size:


710 = 1112 0 0 0 0 0 1 1 1

Step 2: invert the bits


1 1 1 1 1 0 0 0

Step 3: Add bit “1”


1 1 1 1 1 0 0 0
+ 1
2’s compliment
representation of -7 1 1 1 1 1 0 0 1
Page 23
Two’s Complement
Representation
Example 1
Find the 8-bit two’s complement representation of –610 (8 bit word)

Step 1: Find binary representation of the positive value in 8 bits


610 = 000001102

Step 2: Complement the entire positive value

Positive Value: 00000110


Complemented: 11111001

Page 24
Two’s Complement
Representation
Example 1 (contd….)

Step 3: Add one to complemented value


(complemented)  11111001
(add one)  + 1
11111010

So: -610 = 111110102


(in 8-bit 2's complement form)
Page 25
Two’s Complement
Representation
Example 2
Find the 8-bit 2’s complement representation of 2010

Step 1: Find binary representation of the positive value in 8 bits


2010 = 000101002

20 is positive, so STOP after step 1!

So: 2010 = 000101002


(in 8-bit 2's complement form)

Page 26
Two’s Complement
Representation
Example 3
Find the 8-bit two’s complement representation of -80

Step 1: Find binary representation of the positive


value in 8 bits
8010 = 010100002

-80 is negative, so continue…

Step 2: Complement the entire positive value

Positive Value: 01010000


Complemented: 10101111
Page 27
Two’s Complement
Representation
Example 3 (contd….)
Step 3: Add one to complemented value

(complemented)  10101111
(add one)  + 1
10110000

So: -8010 = 101100002


(in 8-bit 2's complement form)

Page 28
Decimal to 2’s compliment conversion
Exercise
Convert following decimal value into their 2’s compliment values
using 8-bit and 10-bit word sizes:
a) -123
b) 100
c) -13
d) 77

Answers (8-bit) Answers (10-bit)


a) 10000101 a) 1110000101
b) 01100100 b) 0001100100
c) 11110011 c) 1111110011
d) 01001101 d) 0001001101
Page 29
Steps in converting 2’s compliment
value to a decimal value
1. Determine if the 2’s compliment value is negative or
positive (check the left most bit)
2. If positive (left most bit=0) then,
i. Simply convert the binary value in the word to decimal
3. If negative (left most bit=1) then,
i. Invert all the bits in the word
ii. Add bit 1
iii. Convert the result to a decimal
iv. Indicate the decimal value as negative

Page 30
Two’s Complement to Decimal
Example 1
Find the decimal equivalent of the 8-bit 2’s complement
value 111011002
Determine if number is positive or negative:

Leftmost bit is 1, so number is negative.

Step 1: Invert the bits 11101100  00010011

Step 2: then Add 1: 00010011


+ 1
Page 31
Two’s Complement to Decimal
Example 1 (contd…)

Step 3: Determine the decimal value:


000101002  2010

So: 11101100 = -2010


(8-bit 2's complement form)

Page 32
Two’s Complement to Decimal
Example 2
Find the decimal equivalent of the 8-bit 2’s
complement value 010010002

Step 1: Determine if number is positive or negative:


Leftmost bit is 0, so number is positive.

Step 2: Determine the numeric value:


010010002 = 64 + 8 = 7210

So: 010010002 = 7210


(8-bit 2's complement form)
Page 33
Exercise (2’s compliment to decimal)

Exercise
Convert following 2’s compliment values to equivalent decimal value:
a) 01011010
b) 10100101
c) 11111010
d) 00110111

Answers
a) 90
b) -91
c) -6
d) 55
Page 34
2’s compliments Additions

Rule 1  Discard if any carryout, i.e any value falling


outside (left of) the word length

Page 35
Exercise - 2’s compliments addition

Exercise

Add the following numbers using 2’s complement and a 8 bit


word size:

1. 44 + 45

2. -75 + 59

3. (-19) + (-7)

4. 10 + (-3)

5. 1 + (-1)
Page 36
2’s compliments Addition
Answers

NOTE: A carry to the leftmost should be ignored.


Page 37
2’s compliments Additions - overflow

Overflow: occurs in addition when two numbers


are added and the answer is too large to fit into the
given word size.

Page 38
Exercise - 2’s compliments addition

Exercise

Add the following numbers using 2’s complement and a 8 bit


word size:

1. 104 + 45

2. 127 + 1

3. (-103) + (-69)

Page 39
2’s compliments addition overflow
errors

The rules for detecting overflow in a two's complement sum are simple:

1.If the sum of two positive numbers yields a negative result, the sum has
overflowed.
2.If the sum of two negative numbers yields a positive result, the sum has
overflowed. Page 40
Two’s Complement Benefits

Benefits of Twos Complements:


 Addition and subtraction are simplified
in the two’s-complement system,
 -0 has been eliminated. Only one zero
representation.

Page 41
Floating Point
Numbers
Floating Point Numbers
 In real life we also need to be able represent
numbers with fractional parts (like: -12.5 &
45.39).
 When dealing with numbers with fractional
parts, computers use a technique, known as
floating point representation (FP).
 In computers, Floating Point numbers are
represented using IEEE 32-bit floating point
format.
Page 43
Floating Point Numbers

Page 44
How to convert Binary Floating Point
Numbers to Decimal

Page 45
How to convert Binary Floating Point
Numbers to Decimal

Convert following Binary fraction values to Decimal


• 11.1
• -101.101
• 111.1001
• 10101.1111

Answers

• 11.1 = 3.5
• -101.101 = -5.625
• 111.1001 = 7.5625
Page 46
How to convert Decimal Floating
Point Numbers to Binary

Page 47
How to convert Decimal Floating
Point Numbers to Binary
Exercise
Convert following decimal fraction values to binary
• 12.875
• -29.375
• 10.1

Answers
Same problem exists
• 12.875 = 1100.111 with 10.2, 10.3, … etc
• -29.375 = -11101.011
• 10.1 = 1010.000110011001100110………….

Page 48
Problem storing binary form
 Is there a way to store the radix point????

Page 49
IEEE Floating Point Representation
 IEEE committee came up with a way to store binary floating
point numbers – known as IEEE-32 which also could indicate
the radix point.
 Floating point numbers can be stored into 32-bits, by dividing
the bits into three parts:
sign exponent mantissa

1 bit 8 bits 23 bits

1 2 910 32
Page 50
Normalization of floating point
number
Binary floating point number should be normalized in such
a way that radix point falls to the right of the leftmost bit 1.

Mantissa Exponent

 37.2510 =100101.012 = 1.0010101 x 25

 7.62510 = 111.1012 = 1.11101 x 22


= 0.01012 = 1.01 x 2-2
 0.312510
Page 51
Biasing the Exponent
 The second field of the floating point format is the
exponent.
 The exponent should be stored as an unsigned 8-bit
number, RELATIVE to a bias of 127+.
 If the Exponent is 5, it is stored as (127 + 5) or 132
 132 = 10000100
 If the Exponent is -5, it is stored as (127 + (-5)) or 122
 122 = 01111010
Page 52
Why Exponent is Biased?
 When a floating point number is normalized, the exponent could
either be a negative or positive. That means exponent is a signed
value.

 But, in IEEE-32 format, there is no dedicated space allocated to


store the sign of the exponent.

 Therefore, the exponent is biased to make the exponent an


unsigned value, which make comparisons on exponent values
and other calculations easier for CPU.

Page 53
Note on Mantissa

 Since the mantissa is stored in a 23 bit field:


 If the mantissa of the binary value is shorter
than 23 bits , we need to add zeros to the
right side until all 23 bit locations are filled.

Page 54
Steps in IEEE – 32 Representation of
a decimal value
1. Convert the Decimal floating point number
to a binary floating point number
2. Normalize the binary floating point
number
3. Convert the exponent in to a biased
exponent and convert it to 8 bit binary
value
4. Represent in IEEE-32 format

Page 55
Decimal Floating Point to
IEEE standard Conversion

Example 1
Find the IEEE FP representation of 40.15625

Step 1: Compute the binary equivalent of the whole part and the
fractional part. (i.e. convert 40 and .15625 to their binary equivalents)

So: 40.1562510 = 101000.001012

Step 2: Normalize the number by moving the decimal point to the


right of the leftmost one.

101000.00101 = 1.0100000101 x 25
Page 56
Decimal Floating Point to
IEEE standard Conversion

Example 1 (contd…)
Step 3. Convert the exponent to a biased exponent
127 + 5 = 132

And convert biased exponent to 8-bit unsigned binary:

13210 = 100001002

Step 4. Store the results from steps 1-3:


Sign Exponent Mantissa
(from step 3) (from step 2)
0 10000100 01000001010000000000000
Page 57
Decimal Floating Point to
IEEE standard Conversion

Example 2
Find the IEEE FP representation of –24.75
Step 1. Compute the binary equivalent of the whole part and the
fractional part.

So: -24.7510 = -11000.112

Step 2. Normalize the number by moving the decimal point to the


right of the leftmost one.

-11000.11 = -1.100011 x 24
Page 58
Decimal Floating Point to
IEEE standard Conversion

Example 2 (contd…)

Step 3: Convert the exponent to a biased exponent


127 + 4 = 131

And convert biased exponent to 8-bit unsigned binary:


==> 131 = 10000011

Sign Exponent mantissa


1 10000011
10001100000000000000000 Page 59
Decimal Floating Point to
IEEE standard Conversion
Exercise
Convert following to IEE representation
1. -0.0078125
2. 2.875

Answers

1.

2.
Page 60
S F E

IEEE Floating Point Components


• For example, if - 1.234 x 1025 is a decimal floating point number.

• S= sign (in this case negative)


• F = mantissa (in this case .234)
• E = exponent (here it’s 25).

 S is the sign bit. Like integer formats, 1 indicates a negative number while
zero indicates positive.

 F is the fractional part of the mantissa. The non-fractional part is not stored,
and assumed to be always 1, so the actual mantissa is “1.F” . This method
provides an additionall room of one bit to store the precision of the value.

 While (F) mantissa provides the precision of the number, the scale or
extent of the magnitude is provided by the (E) exponent.
Page 61
Conversation from IEEE standard
to Decimal Floating Point

Page 62
Steps in IEEE- 32 standard to
Decimal Floating Point Conversion

1. Extract the biased exponent and convert to decimal.


2. Unbias the exponent i.e … [biased exponent ] – 127
3. Extract the mantissa and construct the normalized
form of the binary value using the unbiased
exponent.
4. De - normalize the value.
5. Convert to decimal value.
Page 63
IEEE- 32 standard to Decimal
Floating Point Conversion

Example
Sign Exponent Mantissa

1) 1 01111101 01000000..0

2) 0 10000011 10011000..0

Page 64
IEEE standard to Decimal
Floating Point Conversion.

Example 1
Convert the following 32-bit binary number to its decimal floating point
equivalent:

Sign Exponent Mantissa


1 01111101 010..0000

Step 1: Extract the biased exponent and unbias it

Biased exponent = 011111012 = 12510

Unbiased Exponent: 125 – 127 = -2


Page 65
IEEE standard to Decimal
Floating Point Conversion.

Example 1 (contd…)
Step 2: Write Normalized number in the form:

1.MANTISSA x 2Exponent
For our number:
-1. 01 x 2 –2

Step 3: Denormalize the binary number from step 2.


[that is…move the binary point and get rid of (x 2n) part]
(this is a negative exponent –> so move binary point left)
-0.01012
Page 66
IEEE standard to Decimal
Floating Point Conversion.

Example 1 (contd…)
Step 4: Convert binary number to the decimal floating point
equivalent.

-0.01012 = - ( 0.25 + 0.0625)

= -0.312510

Page 67
IEEE standard to Decimal
Floating Point Conversion.

Example 2
Convert the following 32 bit binary number to its decimal floating point
equivalent:

Sign Exponent Mantissa


0 10000011 10011000..0

Step 1: Extract the biased exponent and unbias it

Biased exponent = 10000112 = 13110

Unbiased Exponent: 131 – 127 = 4


Page 68
IEEE standard to Decimal
Floating Point Conversion.

Example 2 (contd…)
Step 2: Write Normalized number in the form:

1.MANTISSA x 2Exponent
For our number:
1.10011 x 2 4

Step 3: Denormalize the binary number from step 2.


[that is…move the binary point and get rid of (x 2n) part]:
(This is a positive exponent –> so move binary point right)
11001.12 Page 69
IEEE standard to Decimal
Floating Point Conversion.

Example 2 (contd…)
Step 4: Convert binary number to the decimal floating point
equivalent.

11001.1 = [16 + 8 + 1 ]+[0.5]

= 25.510

Page 70
IEEE- 32 standard to Decimal
Floating Point Conversion

Exercise
Sign Exponent Mantissa

1) 0 01110111 00000000000..0

1 10000011 001101100100.0

2)
Answers
1. 0.00390625

2. -19.390625
Page 71
Steps in Floating Point number
addition
ADD the normalized values [ 1.000 × 2-1 ] + [ -1.110 × 2-2 ]

1. Ensure all values that required to be added are normalized. If not, normalize the values.
2. In order to perform addition, the exponents of the two values should be equal.
a. If not, lower exponent should be adjusted, and made equal to the higher exponent.
(therefore, adjust the second value such that its exponent matches with the exponent of the first
value.)
-1.110 × 2-2 = -0.1110 × 2-1
3. Add the two values:
[1.000 × 2-1] + [-0.1110 × 2-1] = 0.001 × 2-1
4. Re- Normalize the result (so that final value could be stored in IEEE format)
0.001 × 2-1 = 1.000 × 2-4
5. Round –off the result:
If the mantissa does not fit in to the space reserved for it (23 bits), mantissa has to be
rounded off. (this could occur, especially if the two values are multiplied).
Page 72
Floating Point number addition

Exercise : ADD 1.0101 x 23 + 11.111 x 25


• Normalize the second value:
11.111 x 25 = 1.1111 x 26
• Adjust the first value such that its exponent matches with the exponent of
the second value.
0.0010101 x 26
• Add the two values:

0.0010101 x 26 + 1.1111 x 26 = 10.0001101 x 26


• Re- Normalize the sum:
1.00001101 x 27
Page 73
Floating Point calculations are slow
 It is impossible to directly add two numbers with different exponents; for example:
1.0101 x 23 + 1.1111 x 26
 In order to perform the addition, the exponents must be made equal to the higher value, in this
case exponent and the mantissa of first value adjusted to get 0.0010101 x 2 6
0.0010101 x 26 + 1.1111 x 26 = 10.0001101 x 26.
 Mathematical operations on floating point numbers may result in final value having a de-
normalized mantissa; for example, 10.xxx or 0.1xxx. Therefore, mantissa will now have to be
adjusted and the exponent modified to re-normalize the number.
 Further, after the addition, the final result may need truncations/rounding-off.
 Therefore, FP calculations are complex and have additional steps to be performed compared to
Integer calculations. Hence, FP calculations are slower compared to integer calculations.
 Special designs need to brought into CPU and ALU to perform floating point calculations with
accuracy and speed.

Page 74
Floating Point number limitations

1. LOSS OF ACCURACY - When a decimal floating-point value is


converted to binary floating-point form, there could be errors due to
following reasons:

• Floating point representation cannot accurately


represent a decimal number like 0.1, 0.2 or 0.3 at
all in binary. (because not all decimal fractions can be
represented in binary form in a finite number of bits).

• Floating point representation cannot accurately


represent values such as √2 or 1/3 in binary format.

Page 75
Floating Point number limitations
(contd…)
2. ROUNDING OFF – ERRORS
I. PRECISION LIMITATION
Floating point representation cannot store very large
numbers or very small numbers which exceed more
than 23 positions (since that is the max for mantissa
value)
II. A very significant difference between integer and
floating point calculations is that floating point
calculations do not yield accurate results.
Multiplying two floating point numbers X and Y can
yield an error. This error is due to the way in which
the mantissa is extended during operations (that is
mantissa of the result may go beyond 23 bits).
Page 76
Integer vs Floating
Point Numbers
About Integers….

• The main advantage is that integers are precise. Therefore


performing calculations using integers are fast and
straightforward for a CPU.
• For example, the value 713 is stored in modern computers as a 64-bit value.
The sign bit determines whether the value is positive or negative (0 is positive,
and 1 is negative). The rest of the 63 bits are used to represent the value.
• Computers can represent integers exactly, and also do integer
mathematics precisely.
• Integer values are used in various applications and calculations
related to dates, age, game (team) scores, purchasing
quantities, population and census counts.

Page 78
About Floating Point numbers…..

• Integers are not well suited to scientific and engineering


calculations involving very large or very small numbers.

• The advantage of floating-point arithmetic is that it can


handle very small values (e.g., the mass of an electron)
and very large values (e.g., the mass of a star). Therefore,
floating-point arithmetic is necessary for the vast
majority of scientific and engineering calculations.

• A floating-point number, representation is not very simple


in CPUs as it is not possible to showcase the “radix
point” using binary digits.

• So, the floating-point number is cleverly covered-up. The


“Exponent” with the “Mantissa” is used in a complex Page 79

You might also like