cs 111 – sept. 1 intro to data representation binary numbers –convert binary decimal –convert...

8
CS 111 – Sept. 1 Intro to data representation Binary numbers Convert binary decimal Convert decimal binary • Text ASCII and Unicode • Commitment: For lab: Be sure you understand number conversions For Friday: Please finish reading sections 1.4 and 1.5

Upload: clifford-watkins

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

CS 111 – Sept. 1

Intro to data representation• Binary numbers

– Convert binary decimal

– Convert decimal binary

• Text– ASCII and Unicode

• Commitment:– For lab: Be sure you understand number conversions

– For Friday: Please finish reading sections 1.4 and 1.5

Page 2: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Numbers in binary

• Place value system just like decimal– We understand 278 = (2 * 100) + (7 * 10) + (8 * 1)

• In a binary number:– Each digit is either a 0 or 1– Digits are multiplied by powers of 2, not powers of 10.

• For example, 001110 and 100011:

32 * 16 * 8 * 4 * 2 * 1 * Value

0 0 1 1 1 0 26

1 0 0 0 1 1 35

Page 3: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Powers of 2

• 20 = 1• 21 = 2• 22 = 4• 23 = 8• 24 = 16

…• 210 ~ 1 thousand• 220 ~ 1 million• 230 ~ 1 billion

• Let’s say we have 4 bits.– What is the lowest # ?– What is the highest # ?

• What if we had 5 bits?

• Is there a pattern?

Page 4: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Decimal binary

• One thing to note is that binary numbers are “longer” than decimal.– A 5-digit decimal number may turn out to be 15 bits

long.

• My technique is the “binary store”– All merchandise is priced $1, $2, $4, $8, $16, …– You enter store with some money, say $45.– Goal is to always buy most expensive gift possible.– So, 45 = 32 + 8 + 4 + 1

32 * 16 * 8 * 4 * 2 * 1 *

1 0 1 1 0 1

Page 5: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Another example

• Convert 61 to binary:• Go to binary store with $61…

61 = 32 + 16 + 8 + 4 + 1

Another way to write this is:

61 = 25 + 24 + 23 + 22 + 20

• Our binary answer is 111101.

Page 6: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Numbers in a byte

• A byte is 8 bits• So, how big can an 8-bit binary number be?

• Hexidecimal shorthand– 8/4 = 2 hexidecimal digits per byte– What do the letters ‘a’ – ‘f’ mean?

a = 10, b = 11, c = 12, d = 13, e = 14, f = 15

– Example: 010111102 = 5e in hex.

– Try this one: 1110002 = ______ in hex.

– Try this one: a4c in hex = ________ in binary.

Page 7: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Text

• Fundamental unit is the character.• Each character of a text document is given a

numerical code.• ASCII code

– Contiguous (make it easy to alphabetize)– Case sensitive– One byte per character

• ASCII table (p. 597)– ‘A’ = 65 ‘a’ = 97 ‘0’ = 48– Try encoding the word: “Dog”

Page 8: CS 111 – Sept. 1 Intro to data representation Binary numbers –Convert binary  decimal –Convert decimal  binary Text –ASCII and Unicode Commitment: –For

Unicode

• To support foreign alphabet and misc. symbols.• Extension of ASCII• 16 bits per character, rather than 8

• unicode.org has code charts• Codes are given in hex.