division and multiplication by 2 · lesson02 page 3 . multiplication: signed (2's complement)...

17
                   Division and multiplication by 2 m Division: Unsigned Integers Let a be an nbit unsigned integer and let  m <= n1 a  = a n1 2 n1 +  +  a m 2 m +  + a 0 2 0   Division by 2 m a/2 m = [ a n1 2 n1 +  +  a m 2 m +  + a 0 2 0 ] 2 m a/2 m = [ a n1 2 n1m +  +  a m 2 0 +  + a 0 2 m ] Integer part Fractional part Discard the fractional part a/2 m = [ a n1 2 n1m +  +  a m 2 0 ] a/2 m (after discarding the fractional part) is the same as  shifting the bits of a by m bits to the right In bit notation: a               a n1    a n2    a m    a m1  a 0 a/2 m            0  0  0 0 0 a n1 a n2  a m   discard pad nm bits m bits lsd (least significant digit) You can safely  use an nm bit bus for a/2 m . Padding to the left is needed only if a wider bus is used ECE2560 Lesson02 Page_1 Lesson02 Page 1

Upload: others

Post on 11-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

                   Division and multiplication by 2m

Division: Unsigned Integers

Let a be an n‐bit unsigned integer and let  m <= n‐1

a  = an‐1 2n‐1 + … +  am 2m + … + a0 20  

Division by 2m

a/2m = [ an‐1 2n‐1 + … +  am 2m + … + a0 20] 2‐m

a/2m = [ an‐1 2n‐1‐m + … +  am 20 + … + a0 2‐m]

Integer part Fractional part

Discard the fractional part

a/2m = [ an‐1 2n‐1‐m + … +  am 20 ] 

a/2m (after discarding the fractional part) is the same as  shifting the bits of a by mbits to the right

In bit notation:

a               an‐1    an‐2  … am   am‐1 … a0

a/2m           0  0 … 0 0 0 an‐1 an‐2 … am  

discard

pad

n‐m bitsm bits

lsd (least significant digit)

You can safely  use an n‐m bit bus for a/2m . Padding to the left is needed only if a wider bus is used

ECE2560 Lesson02 Page_1

Lesson02 Page 1

Page 2: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

Multiplication: Unsigned Integersa  = an‐1 2n‐1 + … +  am 2m + … + a0 20  

Multiplication by 2m

a x 2m = [ an‐1 2n‐1 + … + a0 20] 2m

a x 2m = [ an‐1 2n+m‐1 + … +  … + a0 2m]        =  [ an‐1 2n+m‐1 + … +  … + a0 2m + 0  x 2m‐1+  … + 0 x 20]a x 2m is the same as shifting all the bits of a by m bits to the left

In bit notation:

a                                       an‐1    an‐2  … am   am‐1 … a

a x2m                  an‐1    an‐2  … am   am‐1 … a0 0 0 … 0 

n bits m bits

n+m bits

You can safely  use an n+m bit bus for a x 2m . Padding to the left by 0's is needed if a wider bus is used

Insert 0's

ECE2560 Lesson02 Page_2

Lesson02 Page 2

Page 3: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

Division: Signed (2's complement) Integers

For signed integers stored in 2's complement notation the procedure is exactly the same as that for unsigned integers except that we need to be carful about the sign bit. This restricts m to m <= n‐2. Also we want to make sure that after shifting right by m bits the padding on the left hand side is by 0's if the sign of a is positive and by 1's if the sign is negative. The trick is to pad with the highest significant digit of a, i.e., an‐1 which is 0 for positive integers and 1 for negative integers.

In bit notation:

a               an‐1    an‐2  … am   am‐1 … a0

a/2m           an‐1 … an‐1  an‐1 an‐2 … am  

discard

pad

n‐m bitsm bits

lsd (least significant digit)

You can safely  use an n‐m bit bus for a/2m . Padding to the left is needed only if a wider bus is used

ECE2560 Lesson02 Page_3

Lesson02 Page 3

Page 4: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

Multiplication: Signed (2's complement) Integers

Just as in the case of signed division we need to make sure that if we require padding on the left then we pad with an‐1, otherwise signed multiplication by 2m is similar to unsigned multiplication. 

In bit notation:a                                       an‐1    an‐2  … am   am‐1 … a0

a 2m                    an‐1    an‐2  … am   am‐1 … a0 0 0 … 0 

n bits m bits

n+m bits

You can safely  use an n+m bit bus for a x 2m . Padding to the left by an‐1 is needed if a wider bus is used

Insert 0's

ECE2560 Lesson02 Page_4

Lesson02 Page 4

Page 5: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

MSP430G2553

Address Bus

Data Bus

MSP430G2553

ECE2560 Lesson02 Page_5

Lesson02 Page 5

Page 6: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_6

Lesson02 Page 6

Page 7: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_7

Lesson02 Page 7

Page 8: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_8

Lesson02 Page 8

Page 9: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_9

Lesson02 Page 9

Page 10: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_10

Lesson02 Page 10

Page 11: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_11

Lesson02 Page 11

Page 12: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_12

Lesson02 Page 12

Page 13: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_13

Lesson02 Page 13

Page 14: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

                    

ECE2560 Lesson02 Page_14

Lesson02 Page 14

Page 15: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_15

Lesson02 Page 15

Page 16: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

ECE2560 Lesson02 Page_16

Lesson02 Page 16

Page 17: Division and multiplication by 2 · Lesson02 Page 3 . Multiplication: Signed (2's complement) Integers Just as in the case of signed division we need to make sure that if we require

                    

ECE2560 Lesson02 Page_17

Lesson02 Page 17