Number Systems

61
1 CMPE12c Gabriel Hugh Elkaim Number Systems

description

Number Systems. A Brief History of Numbers. From Gonick, Cartoon Guide to Computer Science. Prehistoric Ledgers. Elaborate Finger Counting. Ancient Number Systems. Positional Number Systems. Number Systems. Prehistory Unary, or marks: /////// = 7 /////// + ////// = ///////////// - PowerPoint PPT Presentation

Transcript of Number Systems

Page 1: Number Systems

1CMPE12c Gabriel Hugh Elkaim

Number Systems

Page 2: Number Systems

2CMPE12c Gabriel Hugh Elkaim

A Brief History of Numbers

From Gonick, Cartoon Guide to Computer Science

Page 3: Number Systems

3CMPE12c Gabriel Hugh Elkaim

Page 4: Number Systems

4CMPE12c Gabriel Hugh Elkaim

Page 5: Number Systems

5CMPE12c Gabriel Hugh Elkaim

Prehistoric Ledgers

Page 6: Number Systems

6CMPE12c Gabriel Hugh Elkaim

Page 7: Number Systems

7CMPE12c Gabriel Hugh Elkaim

Elaborate Finger Counting

Page 8: Number Systems

8CMPE12c Gabriel Hugh Elkaim

Page 9: Number Systems

9CMPE12c Gabriel Hugh Elkaim

Page 10: Number Systems

10CMPE12c Gabriel Hugh Elkaim

Page 11: Number Systems

11CMPE12c Gabriel Hugh Elkaim

Ancient Number Systems

Page 12: Number Systems

12CMPE12c Gabriel Hugh Elkaim

Page 13: Number Systems

13CMPE12c Gabriel Hugh Elkaim

Positional Number Systems

Page 14: Number Systems

14CMPE12c Gabriel Hugh Elkaim

Page 15: Number Systems

15CMPE12c Gabriel Hugh Elkaim

Page 16: Number Systems

16CMPE12c Gabriel Hugh Elkaim

Page 17: Number Systems

17CMPE12c Gabriel Hugh Elkaim

Page 18: Number Systems

18CMPE12c Gabriel Hugh Elkaim

Page 19: Number Systems

19CMPE12c Gabriel Hugh Elkaim

Number Systems

• PrehistoryUnary, or marks:

/////// = 7/////// + ////// = /////////////

• Grouping lead to Roman Numerals:VII + V = VVII = XII

• Better, Arabic Numerals:7 + 5 = 12 = 1 x 10 + 2

Page 20: Number Systems

20CMPE12c Gabriel Hugh Elkaim

Positional Number System

• Base 10 is a special case of positional number system

• PNS First used over 4000 years ago in Mesopotamia (Iraq)– Base 60– 0...59 (written as 60 different symbols)– 5,4560 = 5 x 60 + 45 = 34510

• Positional Number Systems are great for algebra

• Why?

Page 21: Number Systems

21CMPE12c Gabriel Hugh Elkaim

Arabic Numerals

• 345 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-9• Base (or radix): 10

Page 22: Number Systems

22CMPE12c Gabriel Hugh Elkaim

Try multiplication in (non-positional) Roman numerals!

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

Positional Number System

The Mesopotamians wouldn’t

have had this problem!!

*

+

Page 23: Number Systems

23CMPE12c Gabriel Hugh Elkaim

• There are many ways to “represent” a number• Representation does not affect computation result

LIX + XXXIII= LXXXXII (Roman)59 + 33 = 92(Decimal)

• Representation affects difficulty of computing results• Computers need a representation that works with fast electronic circuits• Positional numbers work great with 2-state devices

Positional Number System

Page 24: Number Systems

24CMPE12c Gabriel Hugh Elkaim

Page 25: Number Systems

25CMPE12c Gabriel Hugh Elkaim

Page 26: Number Systems

26CMPE12c Gabriel Hugh Elkaim

What ’10’ Means

Page 27: Number Systems

27CMPE12c Gabriel Hugh Elkaim

Number Base Systems

Page 28: Number Systems

28CMPE12c Gabriel Hugh Elkaim

Binary Numbers

Page 29: Number Systems

29CMPE12c Gabriel Hugh Elkaim

Page 30: Number Systems

30CMPE12c Gabriel Hugh Elkaim

The Powers of 2

Page 31: Number Systems

31CMPE12c Gabriel Hugh Elkaim

Page 32: Number Systems

32CMPE12c Gabriel Hugh Elkaim

Equivalent Numbers

Page 33: Number Systems

33CMPE12c Gabriel Hugh Elkaim

Converting Binary to Decimal

Page 34: Number Systems

34CMPE12c Gabriel Hugh Elkaim

•Base (radix): 2•Digits (symbols) allowed: 0, 1

•Binary Digits, or bits•10012 is really

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

910

•110002 is really1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 0 x 20

2410

Binary Number System

Page 35: Number Systems

35CMPE12c Gabriel Hugh Elkaim

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

Binary Number System

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

Page 36: Number Systems

36CMPE12c Gabriel Hugh Elkaim

Octal Number System• Base (radix): 8• Digits (symbols): 0 – 7• 3458 is really

– 3 x 82 + 4 x 81 + 5 x 80

– 192 + 32 + 5– 22910

• 10018 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 (0345 or 01001).

Page 37: Number Systems

37CMPE12c Gabriel Hugh Elkaim

Hexadecimal Number System

• Base (radix): 16• Digits (symbols) allowed: 0 – 9, a –

fHex Decimal

a 10

b 11

c 12

d 13

e 14

f 15

Page 38: Number Systems

38CMPE12c Gabriel Hugh Elkaim

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 System

Some Examples of converting hex numbers to decimal

Page 39: Number Systems

39CMPE12c Gabriel Hugh Elkaim

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

1 x 256 + 12 x 16256 + 19244810

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

Hexadecimal Number System

Page 40: Number Systems

40CMPE12c Gabriel Hugh Elkaim

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

Value = Σ (Sibi) n-1

i=0

Positional Number System

Page 41: Number Systems

41CMPE12c Gabriel Hugh Elkaim

More PNS fun

• 21203 =

• 4035 =

• 2717 =

• 3569 =

• 11102 =

• 2A612 =

• BEEF16 =

=6910

=10310

=4110

=29410

=1410

=41410

=4887910

Page 42: Number Systems

42CMPE12c Gabriel Hugh Elkaim

Decimal Binary Conversion• Divide decimal value by 2 until the value is 0 • Know your powers of two and subtract

… 256 128 64 32 16 8 4 2 1• Example: 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?

Page 43: Number Systems

43CMPE12c Gabriel Hugh Elkaim

02

1

2

2

2

5

2

10

2

21

2

43

2

86

2

172

2

345345

10101

1001

b10101100134510

Decimal Binary Conversion

Page 44: Number Systems

44CMPE12c Gabriel Hugh Elkaim

Decimal Binary Conversion

• 12810=

• 31010=

• 2610=

Page 45: Number Systems

45CMPE12c Gabriel Hugh Elkaim

Binary Octal Conversion• Group into 3’s starting at least significant symbol

• Add leading 0’s if needed (why not trailing?)

• Write 1 octal digit for each group• Examples:

100 010 111 (binary) 4 2 7 (octal)

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

Page 46: Number Systems

46CMPE12c Gabriel Hugh Elkaim

Octal Binary Conversion

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

Octal Binary

0 000

1 001

2 010

3 011

4 100

5 101

6 110

7 111

Page 47: Number Systems

47CMPE12c Gabriel Hugh Elkaim

Binary Hex Conversion

• Group into 4’s starting at least significant symbol| Adding leading 0’s if needed

• Write 1 hex digit for each group• Examples:

1001 1110 0111 0000 9 e 7 0

0001 1111 1010 0011 1 f a 3

Page 48: Number Systems

48CMPE12c Gabriel Hugh Elkaim

Hex Binary Conversion

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

Example: 3 9 c 8 0011 1001 1100 1000

Page 49: Number Systems

49CMPE12c Gabriel Hugh Elkaim

Conversion TableDecimal Hexadecimal Octal Binary

0 0 0 0000

1 1 1 0001

2 2 2 0010

3 3 3 0011

4 4 4 0100

5 5 5 0101

6 6 6 0110

7 7 7 0111

8 8 10 1000

9 9 11 1001

10 A 12 1010

11 B 13 1011

12 C 14 1100

13 D 15 1101

14 E 16 1110

15 F 17 1111

Page 50: Number Systems

50CMPE12c Gabriel Hugh Elkaim

Page 51: Number Systems

51CMPE12c Gabriel Hugh Elkaim

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?

Page 52: Number Systems

52CMPE12c Gabriel Hugh Elkaim

Negative Integers

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

parentheses: (2000) or color 2000• Sign-magnitude format• Example: -1000 in hex?

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

-3E816

Page 53: Number Systems

53CMPE12c Gabriel Hugh Elkaim

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.41422210

Most accurate approximation until the Renaissance

Page 54: Number Systems

54CMPE12c Gabriel Hugh Elkaim

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

Radix point

Generalized Representation

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

Position is the power

Page 55: Number Systems

55CMPE12c Gabriel Hugh Elkaim

Fractional Representation

• What is 3E.8F16?

• How about 10.1012?

= 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

Page 56: Number Systems

56CMPE12c Gabriel Hugh Elkaim

More PNS Fractional Fun

• 21.0123 =

• 4.1335 =

• 22.617 =

• A.3A12 =

Page 57: Number Systems

57CMPE12c Gabriel Hugh Elkaim

Converting Decimal Binary fractions

•Consider left and right of the decimal point separately.

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

•Use the following table/algorithm to convert the fraction

Page 58: Number Systems

58CMPE12c Gabriel Hugh Elkaim

Fraction Fraction x 2 Digit left of decimal point

0.8 1.6 1 most significant (f-1)

0.6 1.2 1

0.2 0.4 0

0.4 0.8 0

0.8 (it must repeat from 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

Page 59: Number Systems

59CMPE12c Gabriel Hugh Elkaim

What is 2.210 in:

•Binary

•Hex

Page 60: Number Systems

60CMPE12c Gabriel Hugh Elkaim

Page 61: Number Systems

61CMPE12c Gabriel Hugh Elkaim