csc255-702/703 cti/depaul1 csc-255 lecture 3 text and numerical storage (chapter 1 from brookshear)...

39
CSC255-702/703 CTI/DePaul 1 CSC-255 Lecture 3 Text and Numerical Storage (Chapter 1 from Brookshear) Modified by Ufuk Verun from Jim Janossy © 2002, DePaul University CTI – Chicago and Brookshear © 2003 Pearson Education, Inc.

Post on 19-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

CSC255-702/703 CTI/DePaul 1

CSC-255

Lecture 3

Text and Numerical Storage(Chapter 1 from Brookshear)

Modified by Ufuk Verun from

Jim Janossy © 2002, DePaul University CTI – Chicagoand

Brookshear © 2003 Pearson Education, Inc.

CSC255-702/703 CTI/DePaul 2

Resources

This PowerPoint presentation is available for download at www.depaul.edu/~uverun/CSC255/fall2002/slides/Lecture_03.pptTextbook: Chapter 1Print slides at 6 slides/page, avoid waste!Exams are based on slide content, homework and assigned readings

CSC255-702/703 CTI/DePaul 3

Text

Printable characters A, a, B, b, etc.Each different character is assigned a unique 8-bit pattern See the ASCII chart on page 499, Appendix

AASCII: American Standard Code for Information Interchange

More recent codings use more bits to store more symbols and characters Unicode for Java, 16 bits per character

CSC255-702/703 CTI/DePaul 4

ASCII

Developed in 1968 originally as a 7-bit code 7 bits can define 128 patterns,

27=128 Extended to 8 bits, 28=256 patterns

Most common code in useEBCDIC is analogous IBM mainframe code, 8-bit, different assignment of codes to characters

CSC255-702/703 CTI/DePaul 5

ASCII Example

Text “Hello.” coded in ASCII:

CSC255-702/703 CTI/DePaul 6

Unicode

Developed in 1990’s as a 16-bit code Defines 65,536 patterns, 216

Implemented in JavaReplaces earlier schemes to support non-English characters (such as Chinese and Japanese alphabets)First 128 characters are same as ASCII

CSC255-702/703 CTI/DePaul 7

Exercise: ASCII Practice

What is “ABCabc” in ASCII?Char Hex BitsA 41 0100 0001B 42 0100 0010C 43 0100 0011a 61 0110 0001b 62 0110 0010c 63 0110 0011

CSC255-702/703 CTI/DePaul 8

Exercise: More ASCII Practice

What is “01234” (“ means characters) in ASCII?Char Hex Bits0 30 0011 00001 31 0011 00012 32 0011 00103 33 0011 00114 34 0011 0100

CSC255-702/703 CTI/DePaul 9

ASCII Text Facts

Numbers sort before lettersUppercase letters (ABC) sort before lowercase letters (abc)Numbers represented as 8-bit values are inefficient for storage and arithmetic (12,345 takes 5 x 8 = 40 bits, but binary could store it in fewer bits -– 14 bits)All numbers (0 through 9) start with same bits: 0011

CSC255-702/703 CTI/DePaul 10

Packed Decimal

All numbers (0..9) start with same bits 0011 With proper encoding/decoding, there is no

need to store the first 4 bits for ASCII number storage

Store only the last (unique) 4 bits of each digit, since first 4 bits for numbers are always the same (0011)

Takes about half the storage spaceWhere is the sign (+/-) stored?

CSC255-702/703 CTI/DePaul 11

Exercise: Encoding a Number into Packed Decimal

What is “1,234” in ASCII?Char Hex Bits1 31 0011 00012 32 0011 00103 33 0011 00114 34 0011 0100

First 4 bits (0011) are the same for all decimal numbers (0 through 9)Only the low-order 4 bits are stored

CSC255-702/703 CTI/DePaul 12

Encoding into Packed Decimal…

What is 1,234 in ASCII?Char Hex Bits1 31 0011 00012 32 0011 00103 33 0011 00114 34 0011 0100

Answer: 00010010 00110100

CSC255-702/703 CTI/DePaul 13

Encoding into Packed Decimal…

What is 1,234 in ASCII?Char Hex Bits1 31 0011 00012 32 0011 00103 33 0011 00114 34 0011 0100

Answer: 00010010 00110100

Now packed into 2 bytes!

CSC255-702/703 CTI/DePaul 14

Decoding Packed Decimal

Question: What does 00010010 00110100 represent?Someone has to tell you that the bytes contain a number that was stored in packed decimalYou have to decode, then interpret it

CSC255-702/703 CTI/DePaul 15

Decoding Packed Decimal

Decoding area is createdNumber: 00010010 00110100

0011 0011 0011 0011 4 bytes, with leading 0011 generated in each

CSC255-702/703 CTI/DePaul 16

Decoding Packed Decimal

Decoding area is loadedNumber: 00010010 00110100

0011 00010001 0011 0011 0011 4 bytes, with leading 0011 generated in each

CSC255-702/703 CTI/DePaul 17

Decoding Packed Decimal

Decoding area is loadedNumber: 00010010 00110100

0011 00010001 0011 00100010 0011 0011 4 bytes, with leading 0011 generated in each

CSC255-702/703 CTI/DePaul 18

Decoding Packed Decimal

Decoding area is loadedNumber: 00010010 00110100

0011 00010001 0011 00100010 0011 00110011 0011 4 bytes, with leading 0011 generated in each

CSC255-702/703 CTI/DePaul 19

Decoding Packed Decimal

Decoding area is loadedNumber: 00010010 00110100

0011 00010001 0011 00100010 0011 00110011 0011 01000100

4 bytes, with leading 0011 generated in each

CSC255-702/703 CTI/DePaul 20

Decoding Packed Decimal

Decoding area is interpretedNumber: 00010010 00110100

0011 00010001 0011 00100010 0011 00110011 0011 01000100

1 2 3 4

CSC255-702/703 CTI/DePaul 21

Alternative Number Storages

ASCII text: 8-bit “characters”Packed decimal: 4 bits per digit + signBinary system: base 2 number system Pure binary, positive integers Two’s complement, signed values Excess notation

Floating point: sign-exponent-mantissa

CSC255-702/703 CTI/DePaul 22

Binary System

Base 2 number systems Pure binary, positive integers Two’s complement, signed values Excess notation

All use bits differently than ASCIINothing in the bits says whether they are binary coding or ASCII

CSC255-702/703 CTI/DePaul 23

Number Base Basics

The “base” of a number system is defined by how many different digit symbols it usesDecimal is base 10: 0,1,2,3,4,5,6,7,8,9Binary is base 2: 0,1Octal is base 8: 0,1,2,3,4,5,6,7Hex is base 16: 0 thru 9, A,B,C,D,E,F

CSC255-702/703 CTI/DePaul 24

Number Base Basics …

The “value” of each column in a multi-digit number is determined by the number base it is expressed inExample: What is the decimal value of 1101 in decimal, binary, octal, hex?

CSC255-702/703 CTI/DePaul 25

Number Base 10 - Decimal

You figure how much each column is worth, starting from the rightmost column:

102 101 100

100’s 10’s 1’s x x x

1 0 1

base2 base1 base0

Base=10(decimal)

CSC255-702/703 CTI/DePaul 26

102 101 100

100’s 10’s 1’s 1 0 1

Number Base 10 - Decimal

x100

x10

x1 1

0

100

101

1x1

0x10

1x100

CSC255-702/703 CTI/DePaul 27

22 21 20

4’s 2’s 1’s 1 0 1

Number Base 2 - Binary

x4

x2

x1 1

0

4

5

1x1

0x2

1x4

CSC255-702/703 CTI/DePaul 28

82 81 80

64’s 8’s 1’s 1 0 1

Number Base 8 - Octal

x64

x8

x1 1

0

64

65

1x1

0x8

1x64

CSC255-702/703 CTI/DePaul 29

162 161 160

256’s 16’s 1’s 1 0 1

Number Base 16 - Hexadecimal

x256

x16

x1 1

0

256

257

1x1

0x16

1x256

CSC255-702/703 CTI/DePaul 30

Converting from Base 10 to Other Number Bases

Convert decimal 1,234 to binary - how?Convert decimal 1,234 to hex - how?

Algorithm:1. Divide number by the new number

base2. Record remainder as low order digit3. Replace number with division result

(quotient)4. If quotient is not 0, go back to step 1

and repeat

CSC255-702/703 CTI/DePaul 31

Example: Converting Decimal 1,234 to Binary

Low-order digit

High-order digit

quotient remainder

1234 / 2 = 617 0617 / 2 = 308 1308 / 2 = 154 0154 / 2 = 77 077 / 2 = 38 138 / 2 = 19 0 1001101001019 / 2 = 9 19 / 2 = 4 14 / 2 = 2 02 / 2 = 1 01 / 2 = 0 1

CSC255-702/703 CTI/DePaul 32

Example: Converting Decimal 1,234 to Hex

1234 / 16 = 77 277 / 16 = 4 13=D4 / 16 = 0 4

Low-order digit

High-order digit

quotient remainder

4D2

CSC255-702/703 CTI/DePaul 33

Facts about Binary Numbers

Pure binary system stores positive integers only 8-bit number = 0 to 255 (28) 16-bit number = 0 to 65,535 (216) 32-bit number = 0 to

4,294,967,295 (232)

But no matter how many bits are used, there is always some number too large to be represented

CSC255-702/703 CTI/DePaul 34

Binary Addition

Binary addition facts

Used as carry

CSC255-702/703 CTI/DePaul 35

Binary Addition

Addition is as with decimal numbers

1 0 0 1 0 1 0 0

0 1 0 0 1 1 1 0+

1 1 1 0 0 0 1 0

1

1 4 8

7 8

2 2 6

+

decimalbinary

11 1 1

CSC255-702/703 CTI/DePaul 36

Binary Fractions

Radix point is like decimal point

1 0 1 . 1 0 1

Each column to the right of radix point is worth ½ of column to its left

CSC255-702/703 CTI/DePaul 37

Binary Fractions

CSC255-702/703 CTI/DePaul 38

Addition of Binary Fractions

Just like normal binary numbers, add from rightmost to leftmost, use carry ½

¼ 1/8

12

4

= 2 + ¼ + 1/8 = 2.375

= 4 + ½ + ¼ = 4.75

= 7 + 1/8 = 7.1251 1 1. 0 0 1

1 0 0. 1 1 00 1 0. 0 1 1

1 1

+

CSC255-702/703 CTI/DePaul 39

Exercise: Addition of Binary Fractions

½ ¼ 1/8124

= decimal ?

= decimal ?

= decimal ?

0 1 1 0 1 0 1 0. 0 1 1 1+0 0 1 0 0 0 1 0. 0 1 1 0

? ?

1/168163264128