Data Representation - Department of Computer Science

Download Report

Transcript Data Representation - Department of Computer Science

CS 1428
Binary Representation of
Information
 Detecting Voltage Levels
 Why not 10 levels?


Would be unreliable
Not enough difference between states
 On/Off
 Fully Charged - Fully Discharged
 Magnetized - Demagnetized
2
Bits, Bytes, and so on
 A 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
The Binary Numbering System
 A 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
Binary Representation of Numeric
and Textual Information
 Binary numbering system
 Base-2
 Built from ones and zeros
 Each position is a power of 2
1101 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
 Decimal numbering system
 Base-10
 Each position is a power of 10
3052 = 3 x 103 + 0 x 102 + 5 x 101 + 2 x 100
5
Binary Numbers
 Base 2 - digits 0 and 1
___
25
___
24
___
23
___
22
___
21
___
20
6
7
Calculating in binary
 First practice counting to 2010 in binary
 Practice adding: 10112 + 1012
 Try subtracting: 10112 - 1012
8
Representing Signed Numbers
 What 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
method
 Reserve 1 bit (usually the high-order bit) that represents
the sign
 Rest of the bits make up the magnitude
Sign-and-Magnitude
 Easy to understand but not efficient
 Two representations for zero
 Lot of extra overhead to do binary arithmetic
 Recall 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?
2’s complement
 The high-order bit represents the sign but the
magnitude is computed differently
 Has a single representation for zero
 Has an extra negative number
 No extra overhead for binary arithmetic
 Almost all modern computers use this representation
 Example
Converting Decimal to 2’s
Complement
 Get the binary representation for the absolute value of
the number
 Flip all the bits
 Add 1 to the complement
 Example
-3
00011 // 5-bit binary for absolute value of -3
11100 // all bits flipped
11101 // 1 added to the complement
Converting 2’s Complement to
Decimal
 If the high-order bit is 0 then convert the number in
the usual way
 Else
 Subtract 1
 Flip all bits
 Convert to decimal
 Affix a minus sign
Converting 2’s Complement to
Decimal
 Example
11010 // 2’s complement binary
11001 // 1 subtracted
00110 // bits flipped
-6
// affixed the negative sign
2’s Complement Arithmetic
 Binary addition, discard the final carry
 Example
0011
1110
-------0001
 Be careful of overflow
 For a 5 bit 2’s complement representation


16 is too large!
-17 is too small!
More on 2s Complement
 http://en.wikipedia.org/wiki/Two's_complement
 http://www.hal-pc.org/~clyndes/computer-
arithmetic/twoscomplement.html
Summary
 Things you should be able to do
 Convert binary to decimal
 Convert decimal to binary
 Convert 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