75
1 Number Systems Number Systems Patt and Patel Ch. 2+ Patt and Patel Ch. 2+

1 Number Systems Patt and Patel Ch. 2+. 2 A Brief History of Numbers From Gonick, Cartoon Guide to Computer Science

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

1

Number SystemsNumber Systems

Patt and Patel Ch. 2+Patt and Patel Ch. 2+

2

A Brief History of NumbersA Brief History of Numbers

From Gonick, Cartoon Guide to Computer Science

3

4

5

Prehistoric LedgersPrehistoric Ledgers

6

7

Elaborate Finger CountingElaborate Finger Counting

8

9

10

11

Ancient Number SystemsAncient Number Systems

12

13

Positional Number Positional Number SystemsSystems

14

15

16

17

18

19

Number SystemsNumber Systems

• PrehistoryPrehistory

– Unary, or marks:• /////// = 7• /////// + ////// = /////////////

• Grouping lead to Roman Numerals:Grouping lead to Roman Numerals:

– VII + V = VVII = XII

• Better, Arabic Numerals:Better, Arabic Numerals:

– 7 + 5 = 12 = 1 x 10 + 2

20

Positional Number SystemPositional Number System

• Base 10 is a special case of positional number Base 10 is a special case of positional number systemsystem

• PNS first used over 4000 years ago in PNS first used over 4000 years ago in Mesopotamia (Modern day Iraq)Mesopotamia (Modern day Iraq)– Base 60 (Sexagesimal) – Digits: 0..59 (written as 60 different symbols)– 5,4560 = 5 x 60 + 45 x 1 = 34510

• Positional number systems are great for Positional number systems are great for arithmeticarithmetic

• Why?Why?

21

• 345 is really345 is really

– 3 x 102 + 4 x 101 + 5 x 100

– 3 x 100 + 4 x 10 + 5 x 1– 3 is the most significant symbol (carries the

most weight)– 5 is the least significant symbol (carries the

least weight)

• Digits (or symbols) allowed: 0-9Digits (or symbols) allowed: 0-9

• Base (or radix): 10Base (or radix): 10

Arabic NumeralsArabic Numerals

22

Try multiplication in (non-positional) Roman numerals!

XXXIII (33 in decimal) XII (12 in decimal)---------XXXIIIXXXIIICCCXXX-----------CCCXXXXXXXXXIIIIII-----------CCCLXXXXVI-----------CCCXCVI = 396 in decimal

Positional Number SystemPositional Number System

The Mesopotamians wouldn’t

have had this problem!!

*

+

23

Positional Number SystemPositional Number System

• There are many ways to “represent” a numberThere are many ways to “represent” a number

• Representation does not affect computation Representation does not affect computation resultresult

• LIX + XXXIIILIX + XXXIII= LXXXXII= LXXXXII (Roman Numerals)(Roman Numerals)

• 59 + 33 59 + 33 = 92 = 92 (Decimal)(Decimal)

• Representation affects difficulty of computing Representation affects difficulty of computing resultsresults

• Computers need a representation that works Computers need a representation that works with fast electronic circuitswith fast electronic circuits

• Positional numbers work great with 2-state Positional numbers work great with 2-state devicesdevices

24

25

26

What ’10’ MeansWhat ’10’ Means

27

Number Base SystemsNumber Base Systems

28

Binary NumbersBinary Numbers

29

30

The Powers of 2The Powers of 2

31

32

Equivalent NumbersEquivalent Numbers

33

Converting Binary to Converting Binary to DecimalDecimal

34

Binary Number SystemBinary Number System

• Base (radix): 2Base (radix): 2

• Digits (symbols) allowed: 0, 1Digits (symbols) allowed: 0, 1

• Binary Digits, or bitsBinary Digits, or bits

• 1001100122 is really is really

1 x 23 + 0 x 22 + 0 X 21 + 1 X 20

910

• 110001100022 is really is really

1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 0 x 20

2410

35

Computers multiply Arabic numerals Computers multiply Arabic numerals by converting to binary, multiplying by converting to binary, multiplying and converting back (much as us and converting back (much as us with Roman numerals)with Roman numerals)

Binary Number SystemBinary Number System

So if the computer is all binary how does it multiply 5 by 324 when I type it in the calculator program?

36

Octal Number SystemOctal Number System

• Base (radix): 8Base (radix): 8

• Digits (symbols): 0 – 7Digits (symbols): 0 – 7

• 34534588 is really is really– 3 x 82 + 4 x 81 + 5 x 80

– 192 + 32 + 5– 22910

• 1001100188 is really is really– 1 x 83 + 0 x 82 + 0 x 81 + 1 x 80

– 512 + 1– 51310

• In C, octal numbers are represented with a leading 0 In C, octal numbers are represented with a leading 0 (0345 or 01001).(0345 or 01001).

37

• Base (radix): 16Base (radix): 16

• Digits (symbols) allowed: 0 – 9, a – Digits (symbols) allowed: 0 – 9, a – ff HexHex DecimalDecimal

aa 1010

bb 1111

cc 1212

dd 1313

ee 1414

ff 1515

Hexadecimal Number Hexadecimal Number SystemSystem

38

A316 is really:A x 161 + 3 x 160

160 + 316310

3E816 is really:3 x 162 + E x 161 + 8 x 160

3 x 256 + 14 x 16 + 8 x 1768 + 224 + 8100010

Hexadecimal Number SystemHexadecimal Number System

Some Examples of converting hex Some Examples of converting hex numbers to decimalnumbers to decimal

39

10C16 is really:1 x 162 + 0 x 161 + C x 160

1 x 256 + 12 x 1256 + 1226810

In C, hex numbers are represented with a leading “0x” (for example “0xa3” or “0x10c”).

Hexadecimal Number SystemHexadecimal Number System

40

For any positional number system

• Base (radix): b• Digits (symbols): 0 … (b – 1)• Sn-1Sn-2….S2S1S0

Use summation to transform any base to decimal

Positional Number SystemPositional Number System

Value = Σ (Sibi)n-1

i=0

41

More PNS funMore PNS fun• 2120212033 = =

• 40340355 = =

• 27271717 = =

• 35635699 = =

• 1110111022 = =

• 2A62A61212 = =

• BEEFBEEF1616 = =

=69=691010

=103=1031010

=41=411010

=294=2941010

=14=141010

=414=4141010

=48879=488791010

42

Decimal -> Binary Decimal -> Binary ConversionConversion

• Divide decimal value by 2 until the value is 0 Divide decimal value by 2 until the value is 0

• Know your powers of two and subtractKnow your powers of two and subtract– … 256 128 64 32 16 8 4 2 1

• Example: 42Example: 42– What is the biggest power of two that fits?– What is the remainder?– What fits?– What is the remainder?– What fits? – What is the binary representation?

43

02

1

2

2

2

5

2

10

2

21

2

43

2

86

2

172

2

345345

10101

1001

b10101100134510

Decimal Decimal Binary Binary ConversionConversion

44

Decimal Decimal Binary Binary ConversionConversion

• 1281281010==

• 3103101010==

• 26261010==

45

Base Conversion

Binary -> Octal Binary -> Octal ConversionConversion

• Group into 3’s starting at least significant Group into 3’s starting at least significant symbolsymbol– Add leading 0’s if needed (why not trailing?)

• Write 1 octal digit for each groupWrite 1 octal digit for each group

• Examples:Examples:– 100 010 111 (binary)– 4 2 7 (octal)

– 10 101 110 (binary)– 2 5 6 (octal)

46

Octal -> Binary ConversionOctal -> Binary Conversion

It is simple, just write down the 3-bit binary code for each octal digit

OctalOctal BinaryBinary

00 000000

11 001001

22 010010

33 011011

44 100100

55 101101

66 110110

77 111111

Base Conversion

47

Base Conversion

Binary -> Hex ConversionBinary -> Hex Conversion• Group into 4’s starting at least significant Group into 4’s starting at least significant

symbolsymbol– Adding leading 0’s if needed

• Write 1 hex digit for each groupWrite 1 hex digit for each group

• Examples:Examples:– 1001 1110 0111 0000– 9 e 7 0

– 0001 1111 1010 0011– 1 f a 3

48

Hex -> Binary ConversionHex -> Binary Conversion

Again, simply write down the 4 bit binary code for each hex digit

Example: 3 9 c 8 0011 1001 1100 1000

Base Conversion

49

Conversion TableConversion TableDecimalDecimal HexadecimHexadecim

alalOctalOctal BinaryBinary

00 00 00 00000000

11 11 11 00010001

22 22 22 00100010

33 33 33 00110011

44 44 44 01000100

55 55 55 01010101

66 66 66 01100110

77 77 77 01110111

88 88 1010 10001000

99 99 1111 10011001

1010 AA 1212 10101010

1111 BB 1313 10111011

1212 CC 1414 11001100

1313 DD 1515 11011101

1414 EE 1616 11101110

1515 FF 1717 11111111

Base Conversion

50

51

Hex -> Octal• Do it in 2 steps, hex -> binary -> octal

Decimal -> Hex• Do it in 2 steps, decimal -> binary -> hex

So why use hex and octal and not just binary and decimal?

Base Conversion

52

More Conversion PracticeMore Conversion Practice

53

54

55

• Most humans precede number with Most humans precede number with “-” (e.g., -2000)“-” (e.g., -2000)

• Accountants, however, use parentheses: (2000) or color 2000

• Sign-magnitude formatSign-magnitude format

• Example: -1000Example: -10001010 in hex? in hex?

100010 = 3 x 162 + e x 161 + 8 x 160

= -3E816

Negative IntegersNegative Integers

56

Mesopotamians used positional fractions

Sqrt(2) = 1.24,51,1060 = 1 x 600 + 24 x 60-1 + 51 x 60-2

+ 10 x 60-3

= 1.414222

Most accurate approximation until the Renaissance

Positional FractionsPositional Fractions

57

fn-1 fn-2 … f2 f1 f0 f-1 f-2 f-3 … fm-1

Decimal (radix) point

Generalized RepresentationGeneralized Representation

For a number “f” with “n” digits to the left and “m” to the right of the decimal place

Position is the power

Positional Fractions

58

Fractional RepresentationFractional Representation

• What is 3E.8FWhat is 3E.8F1616??

• How about 10.101How about 10.10122??

= 3 x 161 + E x 160 + 8 x 16-1 + F x 16-2

= 48 + 14 + 8/16 + 15/256

= 1 x 21 + 0 x 20 + 1 x 2-1 + 0 x 2-2 + 1 x 2-3

= 2 + 0 + 1/2 + 1/8

59

More PNS Fractional FunMore PNS Fractional Fun

• 21.01221.01233 = =

• 4.1334.13355 = =

• 22.6122.6177 = =

• A.3AA.3A1212 = =

60

Converting Decimal -> Binary Converting Decimal -> Binary fractionsfractions

• Consider left and right of the Consider left and right of the decimal point separately.decimal point separately.

• The stuff to the left can be The stuff to the left can be converted to binary as before.converted to binary as before.

• Use the following table/algorithm Use the following table/algorithm to convert the fractionto convert the fraction

61

FractionFraction Fraction x 2Fraction x 2 Digit left of decimal Digit left of decimal pointpoint

0.80.8 1.61.6 1 1 most significant ( most significant (ff-1-1))

0.60.6 1.21.2 11

0.20.2 0.40.4 00

0.40.4 0.80.8 00

0.80.8 (it must (it must repeat from repeat from here!!)here!!)

• Different bases have different repeating fractions.• 0.810 = 0.110011001100…2 = 0.11002

• Numbers can repeat in one base and not in another.

For 0.810 to binary

62

What is 2.210 in:

• Binary

• Hex

63

Generic Fractional BaseGeneric Fractional Base

• Separate the whole part to the Separate the whole part to the fractional partfractional part

– Know how to solve the whole part

• For the fractional part, use the same For the fractional part, use the same algorithm of successive algorithm of successive multiplication and take off the whole multiplication and take off the whole partpart

– Sounds harder than it is

64

Let’s do an exampleLet’s do an example

• 4.254.251010 = ?? = ??33

65

Let’s do another oneLet’s do another one

• 4.254.251010 = ?? = ??44

66

And yet another oneAnd yet another one

• 4.254.251010 = ?? = ??55

67

68

Fixed Point NumbersFixed Point Numbers

• Used in digital filtering schemes for Used in digital filtering schemes for small microssmall micros

• Control applications need floating Control applications need floating point numbers, but they can be very point numbers, but they can be very slow on small microsslow on small micros

• Fourier transforms for tone detection Fourier transforms for tone detection implemented on small microsimplemented on small micros

69

Digital FilteringDigital Filtering

70

Low-Pass FilterLow-Pass Filter

• Butterworth digital low pass filterButterworth digital low pass filter

• Attenuated signal frequencies above Attenuated signal frequencies above 1/8 sample rate.1/8 sample rate.

0.29289 z + 0.292890.29289 z + 0.29289

--------------------------------------

z - 0.41421z - 0.41421

71

Difference EquationDifference Equation

72

Bode PlotBode Plot

73

How do you implement How do you implement this?this?

74

Questions?Questions?

75