cs 1428. binary representation of information detecting voltage levels why not 10 levels? would be...

17
CS 1428

Upload: eileen-clarke

Post on 29-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

CS 1428

Page 2: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Binary Representation of InformationDetecting Voltage Levels

Why not 10 levels? Would be unreliable Not enough difference between states

On/OffFully Charged - Fully DischargedMagnetized - Demagnetized

2

Page 3: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Bits, Bytes, and so onA bit is one 0 or 1

Short for “binary digit”A byte is a collection of 8 bits

They named it “byte” instead of “bite” so you couldn’t easily mess up the spelling and confuse it with “bit”.

3

Page 4: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

The Binary Numbering SystemA computer’s internal storage techniques are

different from the way people represent information in daily lives

We see and type numbers and letters.

The computer sees ones and zeros for everything

All information inside a digital computer is stored as a collection of binary data

4

Page 5: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Binary Representation of Numeric and Textual InformationBinary numbering system

Base-2Built from ones and zerosEach position is a power of 2

1101 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20

Decimal numbering systemBase-10Each position is a power of 10

3052 = 3 x 103 + 0 x 102 + 5 x 101 + 2 x 100

5

Page 6: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Binary NumbersBase 2 - digits 0 and 1

___ ___ ___ ___ ___ ___25 24 23 22 21 20

6

Page 7: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

7

Page 8: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Calculating in binaryFirst practice counting to 2010 in binary

Practice adding: 10112 + 1012

Try subtracting: 10112 - 1012

8

Page 9: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Representing Signed NumbersWhat about negative numbers?

We can divide the bit patterns into two halves but we need to be careful What to do for zero? Need to consider binary arithmetic as well

One approach is to use the sign and magnitude methodReserve 1 bit (usually the high-order bit) that

represents the signRest of the bits make up the magnitude

Page 10: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Sign-and-MagnitudeEasy to understand but not efficientTwo representations for zeroLot of extra overhead to do binary arithmeticRecall the ALU from Lecture 1

ALU does binary arithmetic (mainly addition)

We would need to redesign the ALU to do arithmetic with sign-and-magnitude representation

What’s the alternative?

Page 11: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

2’s complementThe high-order bit represents the sign but the

magnitude is computed differentlyHas a single representation for zeroHas an extra negative numberNo extra overhead for binary arithmetic Almost all modern computers use this

representationExample

Page 12: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Converting Decimal to 2’s ComplementGet the binary representation for the

absolute value of the numberFlip all the bitsAdd 1 to the complement Example

-3 00011 // 5-bit binary for absolute value of -311100 // all bits flipped 11101 // 1 added to the complement

Page 13: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Converting 2’s Complement to DecimalIf the high-order bit is 0 then convert the

number in the usual wayElse

Subtract 1Flip all bitsConvert to decimalAffix a minus sign

Page 14: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

Converting 2’s Complement to DecimalExample

11010 // 2’s complement binary11001 // 1 subtracted00110 // bits flipped-6 // affixed the negative sign

Page 15: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

2’s Complement ArithmeticBinary addition, discard the final carryExample

00111110--------0001

Be careful of overflowFor a 5 bit 2’s complement representation

16 is too large! -17 is too small!

Page 16: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

More on 2s Complementhttp://en.wikipedia.org/wiki/Two's_compleme

nthttp://www.hal-pc.org/~clyndes/computer-ari

thmetic/twoscomplement.html

Page 17: CS 1428. Binary Representation of Information Detecting Voltage Levels Why not 10 levels? Would be unreliable Not enough difference between states On/Off

SummaryThings you should be able to do

Convert binary to decimalConvert decimal to binaryConvert 2’s complement binary to decimal Convert decimal to 2’s complement binary

Towards the end of the semester perhaps write a program that does this for you