30
DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Embed Size (px)

Citation preview

Page 1: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

DIGITAL SYSTEMS

Number systems & Arithmetic

Rudolf Tracht and A.J. Han Vinck

Page 2: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Number Systems

• A set of symbols representing digits, S

• A string of symbols represents a value

• Value may be computed from the position of digits in a string (positional number system)

• Example: Decimal number system– base 10 or radix 10– (724.6)10=7x100 + 2x10 + 4x1 + 6x1/10

Page 3: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

examples

• Decimal: r = 10, S = {0, 1, 2, ..., 9}• Binary: r = 2, S = {0,1}• Octal: r = 8, S = {0,1,2,3,4,5,6,7}• Hexadec: r = 16, S = {0, ..., 9,

A,B,C,D,E,F}

• Conversion from one base to another:-By successive division or multiplications by the

radix.10

0126 )51(636261)123( xxx

Page 4: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Positional number system

i1n

mii

m1012n1n

raValue

a....a.aa...aaa

•Base r

•A number is a string

•Significance of a digit in a string is indicated by its position

Page 5: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Example for binary numbers

• A binary number is a weighted number. The structure is

2n-1 23 22 21 20 . 2-1 2-2 2-3 2-n

where n is the number of bits from the binary point.

• Ex: conversion of the binary number 1 1 0 1 0 . 0 1 1

– Weight 24 23 22 21 20 . 2-1 2-2 2-3

– Binary 1 1 0 1 0 . 0 1 1

1 1 0 1 0 . 0 1 1 = 24 + 23 + 21 + 2-2 + 2-3

= 16 + 8 + 2 + 0.25 + 0.125 = 26.375

Page 6: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

2-to-10 and 10-to-2

Converting a binary number to a decimal number is easy:

10101.012 24 22 10 2 1 12 2 (16 4 10.25)10 21.2510

there is a radix point here

What about the way back?

210 )1001110001()625(

0

4

5

6

9

2001000001001

2016001016017

2032017032049

2064049064113

2512113512625

Page 7: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Addition and subtraction of binary numbers

• Same as in school: borrow from preceding neighbor

By example: 1 1 1 1 0 0 1 1 1 1 1 0 0

190 1 0 1 1 1 1 1 0 229 1 1 1 0 0 1 0 1 141 1 0 0 0 1 1 0 1 -46 0 0 1 0 1 1 1 0

331 1 0 1 0 0 1 0 1 1 183 1 0 1 1 0 1 1 1

Page 8: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Multiplication/division

• Examples

– Multiplication: 7 x 5 = 35111 x 101 = 111 + 000 + 11100

= 100011 = 35

– Division: 12 / 4 = 31100 / 100 = 11

Page 9: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Negative numbers

• Signed magnitude form• 1- complement• 2-complement

Page 10: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Signed magnitude representation

• The most significant bit indicates the sign

– 01010101 = +85

– 11010101 = - 85

• Problem is the adding/subtracting of numbers

Page 11: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

1-complement representation

• Binary complement for negative numbers

17 = 00010001 11101110 = -17

119 = 01110111 10001000 = -119

sign

Page 12: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

2-complement representation

• Positive numbers +A

• Negative numbers -A

where

12A r

A2 1r

12A0 r

Page 13: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

4-bit numbers

0 0 000 -8 1 0001 0 001 -1 1 1112 0 010 -2 1 110 3 0 011 -3 1 101

4 0 100 -4 1 100 5 0 101 -5 1 011 6 0 110 -6 1 0107 0 111 -7 1 001

Page 14: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

remarks

• Adding/subtracting

– ignoring any carries beyond the MSB• MSB = most significant bit (largest weight)

– correct as long as range not exceeded

+3 0011 +4 0100+4 0100 -7 1001+7 0111 -3 1101

Page 15: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

implementation

• Note: we represent a negative number –A as

Example: 99 = 01100011-99 = 10011100 + 1 =

10011101

Example: 98 = 01100010-98 = 10011101 + 1 = 10011110

1A1A12A2 1r1r

Page 16: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Overflow detection

• MSB different No problem– Reason: always within range

Example: -3 1101 +3 0011+6 0110 -6 1010+3 0011 -3 1101

• MSB the same MSB addition differentExample: -3 1101 +5 0101

-6 1010 +6 0110-9 0111 = +7 +11 1011 =-5

Page 17: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

In other words

• Calculations are done modulo 2r+1 • Let a and b be positive numbers; 0 a,b < 2r ; - a = 2r+1 – a mod

2r+1

+ a + b+ a + b = c < 2r no problem ( overflow detected by sign comparison)

- a - b- a - b = 2r+1 – a + 2r+1 – b = 2r+1 – (a + b) + 2r+1 = 2r+1 – (a + b)

modulo 2r+1 no problem if (a + b) < 2r ( otherwise overflow detected )

- a + b- a + b = 2r+1 – a + b = b –a modulo 2r+1 for b a= 2r+1 – (a - b ) for b a

Page 18: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Floating point numbers

• Decimal

– Example: + 0.234567 x 109

sign mantissa exponent

Page 19: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Binary floating point (IEEE standard)• Binary:

– single- (32 bits), double- (64 bits), extended precision (80 bits)

S E = exponent F = mantissa 1 8 bits 23 bits

To find E and F, write a binary number as 1. a1 a2 a3 a4 … 2x

– F = a1 a2 a3 a4 …

– E = 127 + x range of x: -126 ≤ x ≤ 128The number 0.0 is represented by all 0’sInfinity is represented by all 1’s in the exponent and 0’s in the mantissa

Number = (-1)S(1+F)(2E-127)

Page 20: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Hexadecimal numbers

• Widely used in computer and microprocessor applications

• Base is 16: Decimal binary hexadecimal

– 0 0000 0– 1 0001 1– 2 0010 2– 3 0011 3– 4 0100 4– 5 0101 5– 6 0110 6– 7 0111 7– 8 1000 8– 9 1001 9– 10 1010 A– 11 1011 B– 12 1100 C– 13 1101 D– 14 1110 E– 15 1111 F

Page 21: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Hexadecimal numbers (count)

0 1 2 3 4 5 6 7 8 9 A B C D E F10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F30 etc.

• i.e. In decimal 1316 means 1 x 16 + 3

2C16 means 2 x 16 + 12

A2B16 means 10 x 162 + 2 x 161 + 11

• With 2 hexadecimal digits we can count up to FF16 = 255 decimal

• Each hexadecimal character represents a 4-bit number!

Page 22: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Hexadecimal numbers (examples)

• Example Binary to hexadecimal

1100 1010 0101 0111 0011 1111 0001 0110 1001

C A 5 7 3 F 1 6 9

• Example Hexadecimal to binary 6BD3 = 6 x 163 + 11 x 162 + 13 x 161 + 3in binary: 0110 1011 1101 0011

Page 23: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

octal numbers

• used in computer and microprocessor applications

• Base is 8: Decimal binary octal

– 0 000 0– 1 001 1– 2 010 2– 3 011 3– 4 100 4– 5 101 5– 6 110 6– 7 111 7– 8 1000 10– 9 1001 11– 10 1010 12– 11 1011 13– 12 1100 14– 13 1101 15– 14 1110 16– 15 1111 17

Page 24: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

octal numbers (count)

0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22 23 24 25 26 27 30 etc.

• i.e. In decimal 138 means 1 x 8 + 3

218 means 2 x 8 + 1

3278 means 3 x 82 + 2 x 81 + 7

• With 2 octal digits we can count up to 778 = 63 decimal

• Each octal character represents a 4-bit number!

Page 25: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

octal numbers (examples)

• Example Binary to octal

100 010 101 111 011 111 001 110 001

4 2 5 7 3 7 1 6 1

• Example octal to binary 6233 = 6 x 83 + 2 x 82 + 3 x 81 + 3in binary: 110 010 011 011

Page 26: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

octal numbers (examples)

• Conversion digital to octal

• Example:

359 decimal = 5 x 82 (320) + 4 x 8 (32) + 7

Page 27: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Binary Coded Decimal

• Conversion table

Decimal BCD0 00001 00012 0010 decimal 35 = 0011 0101 BCD 3 0011 decimal 98 = 1001 1000 BCD4 01005 0101 1000 0110 BCD = 86 decimal 6 01107 01118 10009 1001

Page 28: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Some other codesDecimalBCD 1-out-of-10 Biquinary

Binary coded decimal

0 0000 1000000000 01000011 0001 0100000000 01000102 0010 0010000000 01001003 0011 0001000000 0101000 4 0100 0000100000 0110000 5 0101 0000010000 10000016 0110 0000001000 10000107 0111 0000000100 10001008 1000 0000000010 10010009 1001 0000000001 1010000Application: error detection (check!)

Page 29: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

Gray Code

• By example for binary words of length 3

# codeword0 000 numbers differ by 1 binary

difference 11 0012 0113 010 Homework: design a general code for

length n4 1105 1116 1017 100

Page 30: DIGITAL SYSTEMS Number systems & Arithmetic Rudolf Tracht and A.J. Han Vinck

parity

• An extra bit is added to a string to make the number of 1’s always even (or odd if desired)

– i.e. the parity for 011 is 0 01 1 0 the parity for 111 is 1 1 1 1 1

RESULT: any odd number of changes detectable!