Number representation A number can be represented in binary in many - - PowerPoint PPT Presentation

number representation
SMART_READER_LITE
LIVE PREVIEW

Number representation A number can be represented in binary in many - - PowerPoint PPT Presentation

Number representation A number can be represented in binary in many ways. The most common number types to be represented are: Integers, positive integers one's complement, two's complement, sign-magnitude Decimal numbers with fixed


slide-1
SLIDE 1

Number representation

William Sandqvist william@kth.se

A number can be represented in binary in many

  • ways. The most common number types to be

represented are:

  • Integers, positive integers
  • ne's complement, two's complement, sign-magnitude
  • Decimal numbers with fixed increment

fixed-point numbers

  • Decimal numbers with expanded range

floating-point numbers

slide-2
SLIDE 2

Integers

William Sandqvist william@kth.se

1 1 1 1 1

Positive Integer:

=1⋅26 + 1⋅25 + 1⋅23 + 1⋅22 + 1⋅20 = 109

26 25 24 23 22 21 20 1 1 1 1 1

Negative Integer: = -1⋅27+1⋅26 + 1⋅25 + 1⋅23 + 1⋅22 + 1⋅20 = -19

26 25 24 23 22 21 20 1

  • 27
  • 27
slide-3
SLIDE 3

Multiplication of two positive integers

William Sandqvist william@kth.se

0 0 1 0 * 1 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 + 0 0 1 0 0 0 1 0 1 1 0

2 *11 =22 Shift, add multiplicand or 0

slide-4
SLIDE 4

Multiplication with signbit

William Sandqvist william@kth.se

0 0 1 0 2 * 1 0 1 1

  • 5

0 0 1 0 0 0 1 0 0 0 0 0 + 1 1 1 0 1 1 1 0 1 1 0

  • 10

The sign has a negative weight! => Take the two-complement!

slide-5
SLIDE 5

The signbit the other way around

William Sandqvist william@kth.se

1 0 1 1

  • 5

* 0 0 1 0 2 0 0 0 0 1 1 1 0 1 1 0 0 0 0 + 0 0 0 0 1 1 1 0 1 1 0

  • 10

Signextension! (here to 7 bit’s)

slide-6
SLIDE 6

Multiplication of two negative integers

William Sandqvist william@kth.se

1 0 1 1

  • 5

* 1 0 1 1

  • 5

1 1 1 1 0 1 1 1 1 1 0 1 1 0 0 0 0 + 0 1 0 1 0 0 1 1 0 0 1 +25

Signextension! The sign has a negative weight! => Take the two-complement! Answer 7-bit with sign

slide-7
SLIDE 7

Or so we make it easy for us

William Sandqvist william@kth.se

Use only positive numbers in the multiplication Convert to positive numbers Keep track of the result's sign Two-complement for negative numbers if necessary ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( + ⇒ − ⋅ − − ⇒ + ⋅ − − ⇒ − ⋅ + + ⇒ + ⋅ +

slide-8
SLIDE 8

A simple hardware solution …

William Sandqvist william@kth.se

0 1 1 0 SignA 0 1 1 SignB Fcorrect=SignA ⊕ SignB AN-1 AN-2 A0 ...

HA HA HA ...

2’s complement of Product, when correction is needed.

Fcorrect The correction is done by inverting the bits, And add 1 SignA SignB

slide-9
SLIDE 9

Multiplication (two positive integers)

William Sandqvist william@kth.se

) ( ) ( ) (

1 2 3 4 5 6 7 1 2 3 1 2 3

q q q q q q q q b b b b a a a a = ⋅ FA, carry FA, carry FA, carry AND AND AND AND

slide-10
SLIDE 10

William Sandqvist william@kth.se

) ( ) ( ) (

1 2 3 4 5 6 7 1 2 3 1 2 3

q q q q q q q q b b b b a a a a = ⋅ FA, carry FA, carry FA, carry AND AND AND AND

Multiplication (two positive integers)

slide-11
SLIDE 11

William Sandqvist william@kth.se

) ( ) ( ) (

1 2 3 4 5 6 7 1 2 3 1 2 3

q q q q q q q q b b b b a a a a = ⋅ FA, carry FA, carry FA, carry AND AND AND AND

Multiplication (two positive integers)

slide-12
SLIDE 12

William Sandqvist william@kth.se

) ( ) ( ) (

1 2 3 4 5 6 7 1 2 3 1 2 3

q q q q q q q q b b b b a a a a = ⋅ FA, carry FA, carry FA, carry AND AND AND AND

Multiplication (two positive integers)

slide-13
SLIDE 13

William Sandqvist william@kth.se

) ( ) ( ) (

1 2 3 4 5 6 7 1 2 3 1 2 3

q q q q q q q q b b b b a a a a = ⋅ FA, carry FA, carry FA, carry AND AND AND AND

Multiplication (two positive integers)

slide-14
SLIDE 14

Multiplication (two positive integers)

William Sandqvist william@kth.se 3 2 1 3 2 1 0 7 6 5 4 3 2 1

( ) ( ) ( ) a a a a b b b b q q q q q q q q ⋅ =

7

q

6

q

5

q

4

q

3

q

2

q

1

q q

2

b

3

b b

1

b a a

1

a

1

a

2

a

2

a

3

a

3

a

3

a

3

a

2

a

1

a a

2

a

1

a a

Shift of the multiplicator is done by passing it a step to the left

  • f the grid for

each level 12 FA for 4×4 bits

slide-15
SLIDE 15

Multiplication (two positive integers)

William Sandqvist william@kth.se 3 2 1 3 2 1 0 7 6 5 4 3 2 1

( ) ( ) ( ) a a a a b b b b q q q q q q q q ⋅ =

7

q

6

q

5

q

4

q

3

q

2

q

1

q q

2

b

3

b b

1

b a a

1

a

1

a

2

a

2

a

3

a

3

a

3

a

3

a

2

a

1

a a

2

a

1

a a

Carry is delayed trough 8 stages! TMUL~(3*N-4)*TFA

slide-16
SLIDE 16

Could the bit’s be added in some

  • ther order?

William Sandqvist william@kth.se

slide-17
SLIDE 17

William Sandqvist william@kth.se

Quickie question

Discuss your answer with your bench neighbor!

  • What will happen if one confuses the lines a b Cin ?

Ripple Carry-adder: a) Disaster! b) Don’t care!

in

C a b

1 in

C

1

a

1

b

2 in

C

2

a

2

b

3 in

C

3

a

3

b

in

C a b

1 in

C

1

a

1

b

2 in

C

2

a

2

b

3 in

C

3

a

3

b Here someone has completly confused the lines! s

  • ut

C

1

  • ut

C

2

  • ut

C

3

  • ut

C

1

s

2

s

3

s s

  • ut

C

1

  • ut

C

2

  • ut

C

3

  • ut

C

1

s

2

s

3

s

slide-18
SLIDE 18

William Sandqvist william@kth.se

Quickie question

b) Don’t care!

in

C a b

1 in

C

1

a

1

b

2 in

C

2

a

2

b

3 in

C

3

a

3

b s

  • ut

C

1

  • ut

C

2

  • ut

C

3

  • ut

C

1

s

2

s

3

s

  • The sum bit equals ”odd parity” of the input bits.
  • Carry out bit equals the ”majority value” of the inputbits.

In neither case the bits order is of any significance.

slide-19
SLIDE 19

Could the bit’s be added in some

  • ther order?

William Sandqvist william@kth.se

In this way q2 will get the same value but with a different bit

  • rder!

Carry is fed directly to the second row!

slide-20
SLIDE 20

A quicker solution - Carry-Save Multiplier (BV: page 311)

3 2 1 3 2 1 0 7 6 5 4 3 2 1

( ) ( ) ( ) a a a a b b b b q q q q q q q q ⋅ = Figure 5.45 Multiplier carry-save array.

William Sandqvist william@kth.se

slide-21
SLIDE 21

A quicker solution - Carry-Save Multiplier (BV: page 311)

3 2 1 3 2 1 0 7 6 5 4 3 2 1

( ) ( ) ( ) a a a a b b b b q q q q q q q q ⋅ = Figure 5.45 Multiplier carry-save array. Carry is delayed through 6 stages!

Reduces the delay because the carry is fed directly to the next row!

TMUL~(2*N-2)*TFA

William Sandqvist william@kth.se

slide-22
SLIDE 22

A quicker solution - Carry-Save Multiplier (BV: page 311)

3 2 1 3 2 1 0 7 6 5 4 3 2 1

( ) ( ) ( ) a a a a b b b b q q q q q q q q ⋅ = Figure 5.45 Multiplier carry-save array.

William Sandqvist william@kth.se

Extra tip: A fast Carry- look ahead adder here!

slide-23
SLIDE 23

William Sandqvist william@kth.se

slide-24
SLIDE 24

Multiplication with the constant 2

William Sandqvist william@kth.se

0101*2 = 1010 (5*2=10) 1010*2 = 10100 (-6*2=-12) 01010101*2 = 010101010 (85*2=190) 10010101*2 = 100101010 (-107*2=-214) As with multiplication with 10 in our base 10: 63*10 = 630, -63*10 = -630

slide-25
SLIDE 25

Multiplication with 2n

William Sandqvist william@kth.se

0101*2 = 1010 (5*2=10) 0101*22 = 10100 (5*4=20) 0101*23 = 101000 (5*8=40) 0101*24 = 1010000 (5*16=80) compare multiplication with exponents of 10 in our base 10: 6*10 = 60, 6*100 = 600, 6*1000 = 6000

slide-26
SLIDE 26

Multiplication with 2n

William Sandqvist william@kth.se

A multiplication with 2n can be done by shifting all bits n steps to the left and to fill in with zeros 13×8 can be calculated by shifting (01011) three bits to the right

Result: 01011000 corresponds to (104)10

Note that you need more bits to represent the results!

slide-27
SLIDE 27

Barrel-shifter

William Sandqvist william@kth.se

0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3

A0 A1 A2 A3 B0 B1

Multiplication with (20, 21, 22, 23) – 1, 2, 4, 8 MUX

) ( 1 2 3 1 2 3 4 5 6

1

2 ) ( ) (

B B

A A A A S S S S S S S ⋅ =

0 1 2 3

B0 B1

Multiplexor

slide-28
SLIDE 28

Division between two positive integers (BV page 693 fig 10.21 b)

William Sandqvist william@kth.se

0 1 0 1 1 0 1 0 1 1

  • 1 0

0 0 1

  • 0 0

0 1 1

  • 1 0

1

11/2 = 5 Reminder = 1

b r q b a + =

slide-29
SLIDE 29

A little more detail …

William Sandqvist william@kth.se

0 1 0 1 1 0 0 1 0 1 1

  • 0 0

1 0

  • 1 0

0 0 1

  • 0 0

0 1 1

  • 1 0

1

11/2 = 5 Reminder = 1

b r q b a + =

slide-30
SLIDE 30

William Sandqvist william@kth.se

0 1 0 1 1 1 0 q: b: a: r:

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-31
SLIDE 31

William Sandqvist william@kth.se

0 1 0 1 1

  • 1 0

q: 0 b: a: r:

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-32
SLIDE 32

William Sandqvist william@kth.se

0 1 0 1 1

  • 1 0

q: 0 0 b: a: r:

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-33
SLIDE 33

William Sandqvist william@kth.se

0 1 0 1 1

  • 1 0

q: 0 0 1 b: a: r:

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-34
SLIDE 34

William Sandqvist william@kth.se

0 0 0 1 1

  • 1 0

q: 0 0 1 0 b: a: r:

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-35
SLIDE 35

William Sandqvist william@kth.se

0 0 0 1 1

  • 1 0

q: 0 0 1 0 1 b: a: r:

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-36
SLIDE 36

William Sandqvist william@kth.se

0 0 0 0 1 q: 0 0 1 0 1 r: 1

2 1 5 2 11 + = + = b r q b a

A little more detail …

slide-37
SLIDE 37

The Divider

William Sandqvist william@kth.se

FA FA 1

1 0

FA FA 1

1 0 1 0

1 FA FA 1

1 0

FA

1 0

1 FA FA 1

1 0

FA

1 0

b r q q q q b b a a a a + =

1 1 3 1 1 2 3

subtraction

/ Y N / Y N / Y N / Y N

3

q

2

q

1

q q

1

b

3

a b

1

b b

2

a

1

b b

1

a

1

b b a r

1

r

sign- bit

slide-38
SLIDE 38

Using a multiplier and a ROM for division

William Sandqvist william@kth.se

Q=A/B=A×(1/B)

  • Properties
  • Fast
  • VERY big

multiplicator!

MUL ROM A B 1/B Q How many bytes must the ROM contain if A and B are 8 bit numbers? 11111111.00000000 00000000.11111111 8-bit number

  • nly the int-part

8-bit number

  • nly the fraction

≈16×16 multiplicator

slide-39
SLIDE 39

Division with negative integers

William Sandqvist william@kth.se

Division of negative numbers is quite tricky. One way of performing the division is

Convert to positive numbers Keep track of the result's sign Two-complement for negative numbers if necessary ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( + ⇒ − ⋅ − − ⇒ + ⋅ − − ⇒ − ⋅ + + ⇒ + ⋅ +

slide-40
SLIDE 40

Division with 2

William Sandqvist william@kth.se

01010/2 = 00101 (10/2 = 5) 10100/2 = 11010 (-12/2 = -6) Compare division with 10 in our base 10: 630/10 = 63, -630/10 = -63 0 → 1 →

slide-41
SLIDE 41

Logical and arithmetic shift right

William Sandqvist william@kth.se

There is a distinction between logical and arithmetic shift Logical shift right just shifts right. The bits should not be interpreted as a number. It just fills in with 0's. Arithmetic shift right treats the bits as a number. The sign is retained after the shift. You fill in on the left with copies of the sign bit.

slide-42
SLIDE 42

Division with 2n

William Sandqvist william@kth.se

1010/2 = 101 (10/2=5) 10100/22 = 101 (20/4=5) 101000/23 = 101 (40/8=5) 1010000/24 = 101 (80/16=5) compare division with exponents of 10 in our base 10: 60/10 = 6, 600/100 = 6, 6000/1000 = 6

slide-43
SLIDE 43

Division with 2n

William Sandqvist william@kth.se

A division of 2n can be done by shifting all bits n steps to the right and fill in with zeros Observe that the result could be incorrect, the bit’s lost at shifting could be needed as fractions. 17/4 corresponds to shift 010001 2-bits to the right

Result: 000100 = (4)10 Because (0.25)10 can not be represented, the result is not correct!

slide-44
SLIDE 44

William Sandqvist william@kth.se

slide-45
SLIDE 45

Barrel-shifter

William Sandqvist william@kth.se

0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3

A0 A1 A2 A3 B0 B1

Division with (20, 21, 22, 23) – 1, 2, 4, 8

A4 A5 A6

) ( 1 2 3 4 5 6 7 1 2 3 4 5 6

1

2 / ) ( ) (

B B

A A A A A A A A S S S S S S S =

slide-46
SLIDE 46

Fixed-point numbers

William Sandqvist william@kth.se

Two - complementrepresentation

B= bN-1 . bN-2 ...b1 b0 where bi∈{ 0,1}

bN-1 bN-2 ... b1 b0

Sign Bit

Decimal value

FiP(B)= -bN-1 20

+ bN-2 2-1 + ...+ b1 2-(N-2)+ b0 2-(N-1)

This format is also named QN-1-format or fractional representation

slide-47
SLIDE 47

Fixed-Point Q2-Representation

William Sandqvist william@kth.se

000 001 010 011 100 101 110 111

  • 0.25
  • 0.5
  • 1
  • 0.75

0.75 0.5 0.25

See this as a teaching example : Three-bit fixpointnimber A number system with only 8 numbers is practically unusable!

slide-48
SLIDE 48

Fixed-Point Q2-Representation

William Sandqvist william@kth.se

000 001 010 011 100 101 110 111

  • 0.25
  • 0.5
  • 1
  • 0.75

0.75 0.5 0.25

. 1 00 . 1 75 , 01 . 1 5 , 10 . 1 25 , 11 . 1 00 . 25 , 01 . 5 , 10 . 75 , 11 . − − − −

1 ... ... 1 + ≈ −

slide-49
SLIDE 49

Maximal quantization error

William Sandqvist william@kth.se

  • Maximum Quantization Error:

– Since not all numbers can be represented, quantization errors

  • ccur.
  • 1

100

  • 0.75

101

  • 0.5

110

  • 0.25

111 000 0.25 001 0.5 010 0.75 011

Q2

  • 0.375

MQE: 0.125

0.6 QE: 0.1

slide-50
SLIDE 50

Multiplication i Q-format

William Sandqvist william@kth.se

Multiplication does not cause overflow in Q-format, but can result in loss of precision

(1.10)2 × (0.11)2 = (1.1010)2 (Q4-format) = (1.10)2 (Q2-format)

b2 b1 b0 b2 b1 b0 b4 b3 b2 b1 b0 b5

Q2 Q2 Q4 ×

Generally: Extra signbits

Rounding can be smart ...

keep

slide-51
SLIDE 51

Q-format

William Sandqvist william@kth.se

Fixpointmultiplication and fixpointdivision uses the same hardware as integer multiplication and integer division.

Use: Digital filters - eg. sound Card

slide-52
SLIDE 52

William Sandqvist william@kth.se

slide-53
SLIDE 53

Floating-Point Numbers

William Sandqvist william@kth.se

  • A float is represented with a sign-bit ,

exponent-bits, and a mantissa ( fraktion-bits)

Mantissa Exponent S

Sign-Bit exp m

  • The value is calculated as

FlP(B) = (-1) s * (1.m) * 2±exp

  • Often the exp is biased (has an offset), which

gives

FlP(B) = (-1) s * (1.m) * 2exp-(bias)

slide-54
SLIDE 54

IEEE-754

William Sandqvist william@kth.se

  • The float standard IEEE-754 defines a 32-bit

float as

Mantissa Exponent S

31 exp m

  • The value of an 8-bit exponent is calculated as

FlP(B) = (-1) s * (1.m) * 2exp-(127)

  • Special bit patterns has been reserved to represent

negative and positive zero

30 23 22 exp m 27 20 2-1 2-23

slide-55
SLIDE 55

Floating-Point Numbers (IEEE)

William Sandqvist william@kth.se

  • Exponent Values 1 to 254: normalized non-zero floating-point

numbers; biased exponent (-126...+127)

  • Exponent of zero and fraction of zero: positive or negative zero
  • Exponent of ones and fraction of zero: positive or negative

infinity

  • Exponent of zero and fraction of non-zero: Denormalized

number (true exponent is –126), represent numbers from 0 to 2-126

FlP(B) = (-1) s * (0 .m) * 2-126

  • Exponent of ones with a non-zero fraction: NotANumber

(Exception Condition)

  • There is also a standard for a 64-bit number
slide-56
SLIDE 56

IEEE – 32 bit float

William Sandqvist william@kth.se

By exponents written as exess-127 float numbers can be compared by “size” with ordinary integer arithmetic! Sign 1 bit, exponent 8 bit, significand (mantissa) 23 bit. Will be treated again in the course Computer Technology

Normalized

slide-57
SLIDE 57

Decimal addition example

5 2 5

10 001017654 . 10 017654 . 1 7654 . 101 10 234567 . 1 7 . 123456 ⋅ = ⋅ = = ⋅ = = b a

normalized aligned The number which is least (here b) is shifted (aligned) so both numbers will have the same exponent.

5 5 5

10 235584654 . 1 10 001017654 . 10 234567 . 1 ⋅ ⋅ ⋅

+

William Sandqvist william@kth.se

b a c + =

The result could have to be normalized (shifted). Floating point

  • perations are very

demanding, if you do not have specialized hardware - addition can be more demanding than multiplication!

slide-58
SLIDE 58

Addition with float

William Sandqvist william@kth.se

exp exp

2 2

b frac a frac

b b a a ⋅ = ⋅ =

     ≥ ⋅ + ≥ ⋅ + = + =

− − − − exp exp ) ( exp exp ) (

if , 2 * )) 2 ( ( if , 2 * )) 2 ( (

exp exp exp exp exp exp

a b a b b a b a b a c

b a b frac frac a b a frac frac

  • Given two floating point numbers:
  • The sum of these numbers is :

The number that is least is shifted

slide-59
SLIDE 59

Subtraction with float

William Sandqvist william@kth.se

  • Given two floating point numbers :
  • The difference between these numbers is :

exp exp

2 2

b frac a frac

b b a a ⋅ = ⋅ =

     ≥ ⋅ − ≥ ⋅ − = − =

− − − − exp exp ) ( exp exp ) (

if , 2 * )) 2 ( ( if , 2 * )) 2 ( (

exp exp exp exp exp exp

a b a b b a b a b a c

b a b frac frac a b a frac frac

The number that is least is shifted

slide-60
SLIDE 60

Decimal multiplication example

William Sandqvist william@kth.se

9 8 5 3 5 3

10 564854 , 2 10 04 6485389801 , 25 10 417242 , 5 734612 , 4 10 417242 , 5 10 734612 , 4 ⋅ = ⋅ = ⋅ ⋅ = ⋅ = ⋅ = ⋅ =

+

c c b a b a c

normalize The result has to many digits – round off. Multiplication involves making an addition of exponents and multiplication by fractional parts. The answer must then be normalized (shifted).

slide-61
SLIDE 61

Multiplication with float

William Sandqvist william@kth.se

exp exp

2 2

b frac a frac

b b a a ⋅ = ⋅ =

( )

exp exp

2 * *

b a frac frac

b a b a c

+

⋅ = =

  • Given two floating point numbers :
  • The product of these numbers is :

Much moore simple!

slide-62
SLIDE 62

Division with float

William Sandqvist william@kth.se

  • Given two floating point numbers :
  • The ratio of these numbers is :

exp exp

2 2

b frac a frac

b b a a ⋅ = ⋅ =

( )

exp exp

2 / /

b a frac frac b

a b a c

⋅ = =

slide-63
SLIDE 63

Cleaning after floating point

  • perations ...

William Sandqvist william@kth.se

  • When a floating point operation is complete, the

result must be normalized

  • The mantissa is shifted until the first bit is 1
  • For each shift step the exponent is counted up or down

with 1. – The mantissa bits to the right of the first one is saved

FlP(B) = (-1)s * (1.m) * 2exp-(127)

– If the exponent is 0, the first mantissa bit is 0

FlP(B) = (-1)s * (0.m) * 2-(126)

slide-64
SLIDE 64

Floating Point Unit

William Sandqvist william@kth.se

It takes a lot of code and computation to perform floating point operations with a computer with no hardware support for this. PC computers have had embedded floating point units from the processor 486 (1989).

slide-65
SLIDE 65

Expensive software bug?

William Sandqvist william@kth.se

ESA rocket crashes at launch - 1996 double (64-bit float) signed short int (16-bit 2-complement) The translation was wrong – out of range! Used in the system for horisontal bias Someone had been sleeping during this lecture "aritmetik2"!

slide-66
SLIDE 66

Fixed-Point vs. Floating-Point

William Sandqvist william@kth.se

  • Fixed-Point operations are integer
  • perations and are therefore fast
  • Fixed-point values has to be scaled, which
  • ften leads to loss of precision
  • The cost of building hardware is

significantly greater for floating point processors / counter

slide-67
SLIDE 67

William Sandqvist william@kth.se