number systems cpsc125

40
Number Systems Spring 2010 Dr. David Hyland-Wood University of Mary Washington Monday, January 11, 2010

Upload: david-wood

Post on 15-May-2015

1.641 views

Category:

Education


2 download

DESCRIPTION

Number systems, bases and base conversions (decimal, binary, octal, hexadecimal). This lecture covers material not in the text.

TRANSCRIPT

Page 1: Number Systems CPSC125

Number Systems

Spring 2010Dr. David Hyland-WoodUniversity of Mary Washington

Monday, January 11, 2010

Page 2: Number Systems CPSC125

Information Encoding

We use encoding schemes to represent and store information.

• Roman Numerals: I, IV, XL, MMIV

• Acronyms: UMW, CPSC-125, SSGN-726

• Postal codes: 22401, M5W 1E6

• Musical Note Notation: ♪♫

Encoding schemes are only useful if the stored information can be retrieved.

Monday, January 11, 2010

Page 3: Number Systems CPSC125

Monday, January 11, 2010

Linear A has not been deciphered. This tablet stores information, but it can no longer be retrieved. Think about floppy disks, tape drives, bad handwriting and other forms of “lost” data.

Page 4: Number Systems CPSC125

Decimal Notation

• base 10 or radix 10 ... uses 10 symbols

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

• Position represents powers of 10

• 547310 or 5473

(5 * 103) + (4 * 102) + (7 * 101) + (3 * 100)

• 73.1910 or 73.19

(7 * 101) + (3 * 100) + (1 * 10-1) + (9 * 10-2)

Monday, January 11, 2010

Page 5: Number Systems CPSC125

Monday, January 11, 2010

There is an obvious reason to use base 10.

Page 6: Number Systems CPSC125

Monday, January 11, 2010

The ancient Sumerians used base 12, which is why we inherited a 12-hour clock.

Page 7: Number Systems CPSC125

Binary Notation

• base 2 ... uses only 2 symbols

0, 1

• Position represents powers of 2

• 110102

(1 * 24) + (1 * 23) + (0 * 22) + (1 * 21) + (0 * 20)

• 10.112

(1 * 21) + (0 * 20) + (1 * 2-1) + (1 * 2-2)Monday, January 11, 2010

Page 8: Number Systems CPSC125

Monday, January 11, 2010

Computers are binary all the way down. Electrical signals are “on” or “off” based on voltage. Magnetic storage systems such as hard disc drives are based on magnetism. Optical storage systems are based on depth.

Page 9: Number Systems CPSC125

“On” or “Off”

Monday, January 11, 2010

Computers are binary all the way down. Electrical signals are “on” or “off” based on voltage. Magnetic storage systems such as hard disc drives are based on magnetism. Optical storage systems are based on depth.

Page 10: Number Systems CPSC125

“On” or “Off”

time

voltage threshold

Monday, January 11, 2010

Computers are binary all the way down. Electrical signals are “on” or “off” based on voltage. Magnetic storage systems such as hard disc drives are based on magnetism. Optical storage systems are based on depth.

Page 11: Number Systems CPSC125

“On” or “Off”

time

voltage threshold“On”

“Off”

Monday, January 11, 2010

Computers are binary all the way down. Electrical signals are “on” or “off” based on voltage. Magnetic storage systems such as hard disc drives are based on magnetism. Optical storage systems are based on depth.

Page 12: Number Systems CPSC125

“On” or “Off”

time

voltage threshold“On”

“Off”

10

Monday, January 11, 2010

Computers are binary all the way down. Electrical signals are “on” or “off” based on voltage. Magnetic storage systems such as hard disc drives are based on magnetism. Optical storage systems are based on depth.

Page 13: Number Systems CPSC125

“On” or “Off”

time

voltage threshold“On”

“Off”

10

True

False

Monday, January 11, 2010

Computers are binary all the way down. Electrical signals are “on” or “off” based on voltage. Magnetic storage systems such as hard disc drives are based on magnetism. Optical storage systems are based on depth.

Page 14: Number Systems CPSC125

Octal Notation

• base 8 ... uses 8 symbols

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

• Position represents power of 8

• 15238

(1 * 83) + (5 * 82) + (2 * 81) + (3 * 80)

• 56.728

(5 * 81) + (6 * 80) + (7 * 8-1) + (2 * 8-2)

Monday, January 11, 2010

Page 15: Number Systems CPSC125

Hexadecimal Notation

• base 16 or ‘hex’ ... uses 16 symbols

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

• Position represents powers of 16

• B65F16 or 0xB65F

(11 * 163) + (6 * 162) + (5 * 161) + (15 * 160)

• F3.A916 or 0xF3.A9

(15 * 161) + (3 * 160) + (10 * 16-1) + (9 * 16-2)Monday, January 11, 2010

Page 16: Number Systems CPSC125

Octal and Hexadecimal

• Humans are accustomed to decimal

• Computers use binary

• So why ever use octal or hexadecimal?

• Binary numbers can be long with a lot of digits

• Longer number make for more confusion

• Copying longer numbers allows greater chance for error

• Octal represents binary in 1/3 the number of digits

• Hexadecimal represents binary in 1/4 the number of digits

Monday, January 11, 2010

Page 17: Number Systems CPSC125

Base Conversion

Binary to Decimal

• 110102

• (1*24)+(1*23)+(0*22)+(1*21)+(0*20)

• 16 + 8 + 0 + 2 + 0

• 26

Monday, January 11, 2010

Page 18: Number Systems CPSC125

Base Conversion

Octal to Decimal

• 15238

• (1*83)+(5*82)+(2*81)+(3*80)

• 512+320+16+3

• 851

Monday, January 11, 2010

Page 19: Number Systems CPSC125

Base Conversion

Hexadecimal to Decimal

• B65F16

• (11 * 163) + (6 * 162) + (5 * 161) + (15 * 160)

• 45056+1536+80+15

• 46687

Monday, January 11, 2010

Page 20: Number Systems CPSC125

Base ConversionDecimal to Binary

• 82310

• 823 / 2 = 411 R1(12)

• 411 / 2 = 205 R 1 (112)

• 205 / 2 = 102 R 1 (1112)

• 102 / 2 = 51 R 0 (01112)

• 51 / 2 = 25 R 1 (101112)

• 25 / 2 = 12 R 1 (1101112)

• 12 / 2 = 6 R 0 (01101112)

• 6 / 2 = 3 R 0 (001101112)

• 3 / 2 = 1 R 1 (1001101112)

• 1 / 2 = 0 R 1 (11001101112)

• 11001101112

Monday, January 11, 2010

Page 21: Number Systems CPSC125

Base Conversion

Decimal to Octal

• 823

• 823 / 8 = 102 R 7 (78)

• 102 / 8 = 12 R 6 (678)

• 12 / 8 = 1 R 4 (4678)

• 1 / 8 = 0 R 1 (14678)

• 14678

Monday, January 11, 2010

Page 22: Number Systems CPSC125

Base ConversionDecimal to Hexadecimal

• 82310

• 823 / 16 = 51 R 7 (716)

• 51 / 16 = 3 R 3 (3716)

• 3 / 16 = 0 R 3 (33716)

• 33716

• 250010

• 2500 / 16 = 156 R 4 (416)

• 156 / 16 = 9 R 12 (C416)

• 9 / 16 = 0 R 9 (9C416)

• 9C416

Monday, January 11, 2010

Page 23: Number Systems CPSC125

Base ConversionBinary to Octal

• 11001101112

• (12) (1002) (1102) (1112)

• (18) (48) (68) (78)

• 14678

Octal to Binary

• 14678

• (18) (48) (68) (78)

• (0012) (1002) (1102) (1112)

• 11001101112

Monday, January 11, 2010

Page 24: Number Systems CPSC125

Base ConversionBinary to Hexadecimal

• 10110110010111112

• (10112) (01102) (01012) (11112)

• (B16) (616) (516) (F16)

• B65F16

Hexadecimal to Binary

• B65F16

• (B16) (616) (516) (F16)

• (10112) (01102) (01012) (11112)

• 10110110010111112

Monday, January 11, 2010

Page 25: Number Systems CPSC125

Computing Systems

• Computers were originally designed for the primary purpose of doing numerical calculations.

Abacus, counting machines, calculators

• We still do numerical operations, but not necessarily as a primary function.

• Computers are now “information processors” and manipulate primarily nonnumeric data.

Text, GUIs, sounds, information structures, pointers to data addresses, device drivers

Monday, January 11, 2010

Page 26: Number Systems CPSC125

Codes

• A code is a scheme for representing information.

• Computers use codes to store types of information.

• Examples of codes:

• Alphabet

• DNA (biological coding scheme)

• Musical Score

• Mathematical Equations

Monday, January 11, 2010

Page 27: Number Systems CPSC125

Codes

• Two Elements (always)

• A group of symbols

• A set of rules for interpreting these symbols

• Place-Value System (common)

• Information varies based on location

• Notes of a musical staff

• Bits in a binary number

Monday, January 11, 2010

Page 28: Number Systems CPSC125

Computers and Codes

• Computers are built from two-state electronics.

• Each memory location (electrical, magnetic, optic) has two states (On or Off)

• Computers must represent all information using only two symbols

• On (1)

• Off (0)

• Computers rely on binary coding schemesMonday, January 11, 2010

Quantum computing? Tertiary-state machines? Maybe, but not yet and/or not common. Probably coming, though.

Page 29: Number Systems CPSC125

Computers and Binary

• Decimal

109

• Binary

11011012

• Computer 16-bit word size

0000 0000 0110 11012

• Computer 32-bit word size

0000 0000 0000 0000 0000 0000 0110 11012

Monday, January 11, 2010

Many modern computers now use a 64-bit word size in their CPUs. 231 ~ 4 billion. 263 ~ 9.2 quintillion (~1019). Also IP address sizes (v4 and v6).

Page 30: Number Systems CPSC125

Binary Addition

• 4502 + 1234

• 10001100101102 + 100110100102

• 10110011010002

• 5736

1 1 1 1 1

10001100101102

00100110100102

10110011010002

Monday, January 11, 2010

Page 31: Number Systems CPSC125

Negative Numbers

Sign-Magnitude Format

• Uses the highest order bit as a ‘sign’ bit. All other bits are used to store the absolute value (magnitude)

• Negative numbers have the sign bit set

• Reduces the range of values that can be stored.

• -10910 in 16-bit representation using a sign bit 1000 0000 0110 11012

Monday, January 11, 2010

Page 32: Number Systems CPSC125

Negative Numbers

One’s Complement Format

• Exact opposite of the sequence for the positive value. Each bit is “complemented” or “flipped”

• 109 in 16-bit representation0000 0000 0110 11012

• -109 in 16-bit One’s Complement1111 1111 1001 00102

• This makes mathematical operations difficult

Monday, January 11, 2010

Page 33: Number Systems CPSC125

Negative Numbers

Two’s Complement Format

• Add 1 to One’s Complement

• 109 in 16-bit representation0000 0000 0110 11012

• -109 in 16-bit One’s Complement1111 1111 1001 00102

• -109 in 16-bit Two’s Complement1111 1111 1001 00112

Monday, January 11, 2010

Page 34: Number Systems CPSC125

Negative Numbers

• -109 in 16-bit One’s Complement

1111 1111 1001 00102

• -109 in 16-bit Two’s Complement

1111 1111 1001 00102

0000 0000 0000 00012

1111 1111 1001 00112

+

Monday, January 11, 2010

Page 35: Number Systems CPSC125

• 109 in 16-bit representation

0000 0000 0110 11012

• -109 in 16-bit Two’s Complement:

• Copy everything left of the first ‘1’ including the first ‘1’0000 0000 0110 11012

• Complement (flip) all other bits

1111 1111 1001 00112

Two’s Complement Shortcut

Monday, January 11, 2010

Page 36: Number Systems CPSC125

• 10656 in 16-bit representation

0010 1001 1010 00002

• -10656 in 16-bit Two’s Complement:

• Copy everything left of the first ‘1’ including the first ‘1’0010 1001 1010 00002

• Complement (flip) all other bits

1101 0110 0110 00002

Two’s Complement Shortcut

Monday, January 11, 2010

Page 37: Number Systems CPSC125

Two’s Complement Arithmetic

• 4502 + (-1234)

Convert to binary, 16-bit representations

0001 0001 1001 01102

1111 1011 0010 11102

10000 1100 1100 01002

450210

-123410 +

• This is 17 bits – the highest order bit simply gets dropped!

00001100110001002 = 326810

Monday, January 11, 2010

Page 38: Number Systems CPSC125

0 0 0 1 0 0 0 1 1 0 0 1 0 1 1 0

1 1 1 1 1 0 1 1 0 0 1 0 1 1 1 0

0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 0

1

Input

Output

450210

-123410

326810

Monday, January 11, 2010

Imagine that you are doing this operation in a silicon chip with only 16 pins on each side. The 17th “bit” has nowhere to go! In actuality, the input would be preceded by an “add” command.

Page 39: Number Systems CPSC125

Monday, January 11, 2010

Page 40: Number Systems CPSC125

Credits - CC LicensedDecimal numbers on shoji http://www.flickr.com/photos/pitmanra/1184492148/

Hands http://www.flickr.com/photos/faraz27989/537051865/

Clock http://www.flickr.com/photos/zoutedrop/2317065892/

Credits - Fair UseLinear A tablet http://www.historywiz.com/images/greece/lineara.jpg

T-shirt http://www.thinkgeek.com/tshirts-apparel/unisex/frustrations/5aa9/zoom/

8086 chip http://dl.maximumpc.com/galleries/x86/8086.png

Monday, January 11, 2010