M2 - Data Representation
M2 - Data Representation
And
Arithmetic Algorithms
Binary Arithmetic
Binary
Arithmetic
Binary Addition
The rules for binary addition are
1
1 111 11 1
01101010
00001000
+
10000001
1 11111111
1 1 1 11 0 01 0
Binary Arithmetic
Binary Subtraction
The rules for binary subtraction are
Binary Multiplication
Binary multiplication is similar to decimal multiplication
Each partial product is either 0 (multiplication by 0) or exactly same as the multiplicand (multiplication by 1).
Final product is the addition of all the partial products.
E.g. Multiply 1010 and 1101
Binary Arithmetic
Binary Division
Binary division is obtained using same procedure as decimal division
Type equation here.
1 101 Quotient
E.g. Divide 1110101 by 1001
Divisor 1001 1110101 Dividend
1001
01011
1001
0 0 1 00
0000
01001
1001
0000
Signed Binary Numbers
01011 10100
Both the number are complement of each other.
If one of the number is positive, then the other number will be negative with same magnitude and vice versa
0 1 0 1 1 = 11 1 0 1 0 0 = -11
Signed Binary Numbers
One’s Complement: 1 0 1 0 0
Add one + 1
-----------------
Two’s Complement: 1 0 1 0 1
Subtraction using 2’s Complement
Step4: if there is no carry, take 2’s complement of the sum to get the result and it is negative.
Binary addition of BCD numbers may result into invalid BCD codes i.e. numbers greater than 9
The 9’s complement of a decimal number is obtained by subtracting each digit from 9.
E. g. 9’s complement of 345 is 999-345 = 654
Step 4: If there is End Around Carry, then result is positive and add carry to get the result.
Step 5: If there is no End Around Carry, then take 9’s complement of the sum to get the result and
it is negative.
BCD Subtraction Using 9’s Complement
1
0 1 1 1 1 0 0 0
+
0 1 1 0 0 1 1 0
1 1 1 0 1 1 1 0
0 1 1 0 0 1 1 0
1 0 1 0 01 0 1 0 0
𝟏
1
0 1 0 0 0 1 0 1
BCD Subtraction Using 9’s Complement
Step 4: If there is End Around Carry, then result is positive and discard the carry and get the
result.
Step 5: If there is no End Around Carry, then take 10’s complement of the sum to get the result
and it is negative.
BCD Subtraction Using 10’s Complement
1
0 1 1 1 1 0 0 0
+
0 1 1 0 0 1 1 1
1 1 1 0 1 1 1 1
0 1 1 0 0 1 1 0
1 0 1 0 0 1 0 1 0 1
𝟏
Hexadecimal Arithmetic
Hexadecimal Addition
Add the two hexadecimal digits by working with their decimal equivalent
If the decimal sum is less than 16, write down the hex equivalent.
If the decimal sum is more than 15, then subtract 16 from the sum and write down the hex result and
carry 1 to the next digit.
E.g. Add 5𝐴𝐶 and 𝐵13
Hexadecimal Subtraction
Take 2’s complement of the subtrahend
Add minuend to the 2’s complement of the subtrahend
If the sum is less than 16, write down the hex equivalent.
If the decimal sum is more than 15, then subtract 16 from the sum and write down the hex result and
carry 1 to the next digit.
If the MSB bit is 1, then the result is negative and it is in 2’s complement form.
E.g. Subtract 5𝐴𝐶 and 𝐵13
0101 1010 1100
+ 0100 1110 1101
1010 1001 1001 0101 0110 0111
5 6 7
The answer is −567
Multiplication
1 0 00 1 1 1 1 𝑃𝑟𝑜𝑑𝑢𝑐𝑡 (143)
Multiplication
01100 10011100
×11101 01100011
+ 1
01100
00000 01100100
01100
01100 0 1 1 0 0 1 0 0 → 100
01100 Wrong answer
10011100
Booths Multiplication Algorithm
Booths Algorithm
Booths Multiplication Algorithm
Step 1: Initialize the registers Q = Multiplier, M = Multiplicand, A = 0, 𝑄−1 = 0 and n = count (no.
of bits used to represent multiplicand and multiplier)
Step 2: Set 𝑄0 = 𝐿𝑆𝐵 𝑜𝑓 𝑀𝑢𝑙𝑡𝑖𝑝𝑙𝑖𝑒𝑟
Step 3: The control logic scans the 𝑄0 and 𝑄−1 bits and decides one of the following actions
Step 3.1: If 𝑄0 , 𝑄−1 = 10 then A = A – M (addition of A with 2’s complement of M) go to Step 3.4
Step 3.2: If 𝑄0 , 𝑄−1 = 01 then A = A + M go to Step 3.4
Step 3.3: If 𝑄0 , 𝑄−1 = 00 𝑂𝑅 𝑄0 , 𝑄−1 = 11 them go to step 3.4
Step 3.4: Perform Arithmetic right shift and decrease the count.
𝐴 ← 0, 𝑄−1 ← 0,
𝑀 ← 𝑀𝑢𝑙𝑡𝑖𝑝𝑙𝑖𝑐𝑎𝑛𝑑,
𝑄 ← 𝑀𝑢𝑙𝑡𝑖𝑝𝑙𝑖𝑒𝑟,
𝑛 ← 𝐶𝑜𝑢𝑛𝑡
= 𝟏𝟎 = 𝟎𝟏
𝑄0 , 𝑄−1
= 𝟎𝟎
𝐴←𝐴−𝑀 = 𝟏𝟏 𝐴←𝐴+𝑀
Arithmetic right shift
𝐴, Q, 𝑄−1
𝑐𝑜𝑢𝑛𝑡 = 𝑐𝑜𝑢𝑛𝑡 − 1
𝑵𝒐
𝑐𝑜𝑢𝑛𝑡 = 0?
𝒀𝒆𝒔
Fig. Representation of the Booth's Algorithm End
Booths Multiplication Algorithm
Example 1: Multiply −5 = 1011 and 2 = 0010
Result
11110110
As MSB is 1, the result is negative and is in 2’s complement form. 00001001
+ 1
0 0 0 0 1 0 1 0 = (−𝟏𝟎)𝟏𝟎
Booths Multiplication Algorithm
Example 2: Multiply 14 = 01110 and −5 = 11011
Result
As MSB is 0, the result is positive 0 0 0 1 0 1 0 1 0 0 = (𝟖𝟒)𝟏𝟎
Booths Multiplication Algorithm
Steps:
Check the multiplicand and multiplier to be negative
Negative (multiplier/multiplicand or both)– take 2’s complement of it
Add sign bit in MSB
Initialize A, Q, Q-1 M and Count
Based on values of Q0,Q-1
Perform A-M / A+M
Arithmetic Right shift
Decrease count
Division algorithm can be grouped into two classes according to their iterative operator.
First Class
Subtraction is the iterative operator
Slow division as it’s execution time is proportional to the length of the operand (divisor)
Second Class
Obtain reciprocal of divisor and multiply the result by the dividend
Two ways to find reciprocal
Series expansion
Newton-Raphson iteration
Fast division
Integer Division
Slow Division
Algorithm
Restoring Non-restoring
Binary Binary
Division Division
Integer Division
Left Shift 𝐴, 𝑄
𝐴←𝐴−𝑀
No Yes
𝐴<0
𝑄0 ← 0
𝑄0 ← 1
𝐴←𝐴+𝑀
𝑐𝑜𝑢𝑛𝑡 ← 𝑐𝑜𝑢𝑛𝑡 − 1
No Yes
𝑐𝑜𝑢𝑛𝑡 = 0 End
Fig. Flowchart for Restoring Division
Integer Division
Iteration 𝑨 𝑸 𝑶𝒑𝒆𝒓𝒂𝒕𝒊𝒐𝒏
0 0 0 0 0 0 1 1 1 Initial values
0 0 0 0 1 1 1 Left shift
1 1 0 1 1 1 1 A=A-M
1
1 1 0 1 1 1 1 0 LSB of Q = 0
0 0 0 0 1 1 1 0 Restore A
0 0 0 1 1 1 0 Left shift
1 1 1 0 1 1 0 A=A-M
2
1 1 1 0 1 1 0 0 LSB of Q = 0
0 0 0 1 1 1 0 0 Restore A
Integer Division
Example 1: Divide using restore division method 7/3
Dividend : 𝑄 = (7)10 = (0111)2 Divisor : 𝑀 = (3)10 = (0011)2
Iteration 𝑨 𝑸 𝑶𝒑𝒆𝒓𝒂𝒕𝒊𝒐𝒏
2 0 0 0 1 1 1 0 0
0 0 1 1 1 0 0 Left shift
0 0 0 0 1 0 0 A=A-M
3
0 0 0 0 1 0 0 1 LSB 0f Q = 1
0 0 0 1 0 0 1 Left shift
1 1 1 0 0 0 1 A=A-M
4
1 1 1 0 0 0 1 0 LSB of Q = 0
0 0 0 1 0 0 1 0 Restore A (A=A+M)
𝑨 𝑸
0 0 0 0 1 1 1 0
Integer Division 0 0 0 1 1 1 0
Example 2: Divide 14/6 1 0 1 1 1 1 0
0 0 0 1 1 1 0 0
Dividend : 𝑄 = (14)10 = (1110)2
0 0 1 1 1 0 0
Divisor : 𝑀 = (6)10 = (0110)2
1 1 0 1 1 0 0
0 0 1 1 1 0 0 0
0 1 1 1 0 0 0
0 0 0 1 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1
1 1 0 0 0 0 1
0 0 1 0 0 0 1 0
Integer Division
Result of the division is place in 𝑛-bit 𝑄 register (quotient) and 𝑛-bit 𝐴 register (remainder)
Integer Division
No 𝑀𝑆𝐵 𝑜𝑓 Yes
𝑀𝑆𝐵 = 𝐴0
=0
End
𝑄0 = 0 𝑄0 = 1
𝑄 = 𝑄𝑢𝑜𝑡𝑖𝑒𝑛𝑡
𝐶𝑜𝑢𝑛𝑡 ← 𝐶𝑜𝑢𝑛𝑡 − 1 𝐴 = 𝑅𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟
A finite number can also represented by four integers components, a sign, a base (B), a significand (S),
and an exponent (E).
±𝑆 × 𝐵±𝐸
IEEE 754 Floating Point Representation
The leftmost bit stores the sign of the number ( 0 = 𝑃𝑜𝑠𝑖𝑡𝑖𝑣𝑒 , 1 = 𝑁𝑒𝑔𝑎𝑡𝑖𝑣𝑒 )
The exponent value is stored in the next 8 bits. Biased representation is used.
A fixed value, called as bias is added to the exponent to get biased exponent.
𝐵𝑖𝑎𝑠 = (2𝑘−1 − 1) … 𝑤ℎ𝑒𝑟𝑒 𝑘 = 𝑛𝑜. 𝑜𝑓 𝑏𝑖𝑡𝑠 𝑖𝑛 𝑡ℎ𝑒 𝑏𝑖𝑛𝑎𝑟𝑦 𝑒𝑥𝑝𝑜𝑛𝑒𝑛𝑡
The final portion of the word (23 bits) is the significand.
IEEE 754 Floating Point Representation
Converting a number into Binary Single precision and Double precision format
Floating point numbers are those where position of point is not fixed.
To convert the given number into normalized form, apply normalization process
Shifting the point either to left or right, so that there is only one non-zero digit to the left of point
𝑀 is mantissa or Significand
IEEE 754 Floating Point Representation
Converting a number into Binary Single precision and Double precision format
1. Check the sign of a number.
2. Convert given decimal number into its equivalent binary number
3. Find out normalized number to get value of true exponent and mantissa/significand
4. Calculate the value of biased exponent using respective bias value
5. Convert calculated biased exponent into binary equivalent
6. Represent number in IEEE 754 format
S e M
IEEE 754 Floating Point Representation
Converting a number into Binary Single precision and Double precision format
0.125 × 2 = 0.250
0.250 × 2 = 0.500
0.500 × 2 = 1.000
0.125 10 = 001 2 Hence, 85.125 = 1010101.001 True Exponent
85.125 = 1.010101001 × 26
Sign = 0
Mantissa/Significand
IEEE 754 Floating Point Representation
Converting a number into Binary Single precision and Double precision format
Converting a number into Binary Single precision and Double precision format
0.125 × 2 = 0.250
0.250 × 2 = 0.500
0.500 × 2 = 1.000
0.125 10 = 001 2 Hence, 85.125 = 1010101.001 True Exponent
85.125 = 1.010101001 × 26
Sign = 1
Mantissa/Significand
IEEE 754 Floating Point Representation
Converting a number into Binary Single precision and Double precision format
1 10000000101 01010100100000000000000000000000000000000000000000
IEEE 754 Floating Point Representation
Special Values
IEEE has reserved some values that can ambiguity.
Zero – Zero is a special value denoted with an exponent and
mantissa of 0. -0 and +0 are distinct values, though they both
are equal.
Denormalized – If the exponent is all zeros, but the mantissa
is not then the value is a denormalized number.
Infinity – The values +infinity and -infinity are denoted with an
exponent of all ones and a mantissa of all zeros.
Not A Number (NAN) – The value NAN is used to represent a
value that is an error. This is represented when exponent field
*For Double precision (just replacing 255 by 2049)
is all ones with a zero sign bit or a mantissa that it not 1
followed by zeros.