Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | prosper-strickland |
View: | 215 times |
Download: | 1 times |
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 1
Representing Information in Computers: numbers: counting numbers, integers: signed
magnitude and 2’s complement (slides 2-29) characters: ASCII, etc. (slides 30-33) summary (slides 34)
Data Representation
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 2
base: set of n atomic symbols + interpretation of each symbol as a “value” (quantity)
number: a string of symbols (digits) + interpretation rule
digits are numbered right to left, start at 0• e.g. 4-digit number: d3d2d1d0
“value” of a digit depends on value of symbol and position of digit in string• value of digiti = (value of symboli) ni
“value” of number is sum of value of digits
Base-n Number Systems
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 3
(nothing new here!)
symbols = { 0, 1, 2, 3, , 9} – usual meaning 4-digit example: d3d2d1d0 = 1436
= d3 103 + d2 102 + d1 101 + d0 100
= 1 103 + 4 102 + 3 101 + 6 100
= 1 1000 + 4 100 + 3 10 + 6 1
= 1000 + 400 + 30 + 6
= 143610
Base-10 Example
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 4
set of symbols = { 0 , 1 } – usual meaning 4-digit number example: 10112
= 1 23 + 0 22 + 1 21 + 1 20
= 1 8 + 0 4 + 1 2 + 1 1
= 8 + 0 + 2 + 1
= 1110
Base-2 (binary) Number System
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 5
Binary Decimal Binary Decimal 0000 0 1000 8 0001 1 1001 9 0010 2 1010 10 0011 3 1011 11 0100 4 1100 12 0101 5 1101 13 0110 6 1110 14 0111 7 1111 15
Binary – Decimal Conversion Table
Memorize these!
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 6
What about the range of representation?• n-bit values can represent (at most) 2n different
counting numbers (why?)• if start representation at 0, then largest value
represented is 2n – 1 (why?)• recall 4-bit example: ( 24 = 16 )
– range = 0 . . 15
Range of Representation
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 7
Some Observations:• binary representation of counting numbers often
requires lots of bits
– example: 60110 = 10010110012 (10 bits!)
• converting binary decimal is not trivial• binary can be awkward and error prone for use by
people!
More about Binary
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 8
16 symbols = { 0 . . 9, A, B, C, D, E, F}• interpret: 0 . . 9 – usual meaning• A 10 B 11 C 12• D 13 E 14 F 15
Why use hexadecimal (hex) notation? shorthand for binary notation converting binary hexadecimal is easy
Hexadecimal (Base-16) Number System
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 9
Converting Binary Hex 4 binary digits make up the value of one hexadecimal
digit: • hex digits range 0 . . 15• 4-bit binary values range 0 . . 15
2 Steps• group bits in 4’s starting from least significant• replace each group by corresponding hex digit
Converting Hex Binary replace each hex digit by 4-digit binary rep
Converting Binary Hex
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 10
16-bits: 10001010011011102 (= 35,43810)1. group in 4’s 1000 1010 0110 1110
2. replace with hex 8 A 6 E 16
10-bits: 10010110012 (= 60110)1. group in 4’s 0010 0101 1001
2. replace with hex 2 5 9 16
Binary => Hex Examples
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 11
Binary Decimal Hex Binary Decimal Hex 0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 10 A
0011 3 3 1011 11 B
0100 4 4 1100 12 C
0101 5 5 1101 13 D
0110 6 6 1110 14 E
0111 7 7 1111 15 F
Binary / Decimal / Hexadecimal
Memorize these!
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 12
Encoding of More Types of Information counting numbers, integers what are they? are these different from base-n systems?
Encoding of Counting Numbers (0, 1, 2, 3, …) simple mapping: binary number system! base-2 number system
Encoding of Integers positive and negative values must encode sign & magnitude not so easy!
Other Encodings
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 13
use most significant bit (leftmost) to encode sign 0 = positive, 1 = negative use remaining n – 1 bits to encode magnitude using
binary number system (as for counting numbers) example: 8-bit signed-magnitude
100000012
magnitude = 1sign bit = 1 negative number
Thus this represents value – 1
Signed Magnitude Representation
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 14
Problems with Signed-Magnitude Encoding: two representations of 0!?
• positive 0 (sign bit = 0)• negative 0 (sign bit = 1)• is zero positive or negative?
performing arithmetic operations with signed-magnitude values can be awkward
signed-magnitude not often used in practice
Signed Magnitude (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 15
complement of one bit: b = 1 – b • complement of 0 = 1; complement of 1 = 0
complement of an n-bit value:• complement each bit
2’s complement of an n-bit value:• complement each bit, and then add 1• ignore any carry out of most significant bit (why?)
Binary Addition and Subtraction:
0 + 0 = 0; 1 + 0 = 1; 0 + 1 = 1; 1 + 1 = 0 (carry 1) 0 – 0 = 0; 1 – 0 = 1; 0 – 1 = 1 (borrow 1); 1 – 1 = 0
2’s Complement Encoding
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 16
1. original value = 100111010010 complement each bit: 011000101101 add 1 +1 2’s complement rep = 011000101110
2. original value = 0001 complement each bit: 1110 add 1 +1 2’s complement rep = 1111
2’s Complement Examples
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 17
Assume we have n bits available. use half of binary values for negative values (i.e. 2n–1
values), the rest for non-negative encode positive values (and 0) the same way as
counting numbers range 0 . . 2n – 1 – 1 uses all values that start with 0 use 2’s complement as negation operation! encoding of – “x” = 2’s complement of “x”
Encoding Integers in 2’s Complement
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 18
1. 8-bit 2’s complement encoding of –1
+110 0000 0001
complement: 1111 1110
add 1 + 1
so – 110 = 1111 1111
the largest binary representation is for the smallest negative number!
Integer Encoding Examples
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 19
2. 8-bit 2’s complement encoding of –128
+12810 1000 0000complement: 0111 1111add 1 + 1
–12810 1000 0000 the smallest binary representation starting with 1 (indicating
negative) is for the largest negative number that can be represented!
do we have two values with the same representation? –12810 = +12810 ? No, as +12810 cannot be represented in 8-bit 2’s complement (see next page!)
Integer Encoding Examples (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 20
range of numbers that can be represented is:
– 2n – 1, …, – 1, 0, …, 2n – 1 – 1 e.g. for 8-bit 2’s complement encodings:
n = 8: 28 = 256, 27 = 128 range: – 2n – 1, …, 2n – 1 – 1 = – 128, …, 127 remember: all negative value encodings start with
most significant bit = 1
We assume 8 bit representation in the following slides!
2’s Complement Range of Representation
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 21
Encoding of negative numbers seems strange!? Why do it like this?!
We want A. Negating a negative number = same positive
number we started with B. A single representation of zero C. Math (addition, subtraction, etc.) to work without
keeping track of representation being used (i.e. regular counting numbers vs. 2’s complement)
More on 2’s Complement
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 22
A. Negating a negated number should give the original number:
with 2’s complement does – (– x) = x?
Example 1: (8 bits)
– 110 1111 1111complement 0000 0000add 1 + 1
+110 0000 0001 – ( – 1 ) = 1 it works!
2’s Complement (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 23
A. (continued) Negating a negated number should give the original number:
Special Case!Example 2: (8 bits)
– 12810 1000 0000complement 0111 1111add 1 + 1
– 12810 1000 0000 (we saw this already!)
Should be +12810 (which cannot be represented in 8 bits 2’s complement)
What happened? Overflow (more later!)
2’s Complement (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 24
B. How many representations are there for 0 in 2’s complement?
010 0000 0000
complement 1111 1111
add 1 + 1 (ignore carry out of “1”)
010 0000 0000
2’s Complement (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 25
C. (Example) Add: 1810 + 7510 (should be 9310 )Convert to Hex and Binary:
1810 = 1*16 + 2 = 12h = 0001 00102
7510 = 4*16 + 11 = 4Bh = 0100 10112
Add in Hex and Convert back to Decimal:
12h + 4Bh = 5Dh = 5*16 + 13 = 9310
Add in Binary and Convert back to Decimal:
0001 00102 + 0100 10112 = 0101 11012 =
1*64 + 1*16 + 1*8 + 1*4 + 1*1 = 9310
BinaryAddition worked!
Binary Math Example
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 26
C. (continued) What if the binary numbers on the previous page were in 2’s complement representation?
0001 00102
0100 10112
0101 11012
No difference as all these numbers are positive (most significant bit is “0”)
2’s Complement Math (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 27
Add: 1010 + 23710 (should be 24710 )
Convert to Hex and Binary:
1010 = 0*16 + 10 = 0Ah = 0000 10102
23710 = 14*16 + 13 = EDh = 1110 11012
Add in Hex and Convert back to Decimal:
0Ah + EDh = F7h = 15*16 + 7 = 24710
Add in Binary and Convert back to Decimal:
0000 10102 + 1110 11012 = 1111 01112 =
1*128 + 1*64 + 1*32 + 1*16 + 1*4 + 1*2 + 1*1 = 24710
As expected, BUT...
More Binary Arithmetic
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 28
C. (continued) What if these binary numbers were in 2’s complement representation?
0000 10102 positive number so no change = 1010
1110 11012 negative number so complement and add 1
= – (0001 00102 + 1) = – 0001 00112 = – (1*16 + 1*2 + 1*1)10
= – 1910
Thus we would want the answer to be:
1010 + – 1910 = – 910
converting – 910 to 2’s complement gives:
0000 10012 + 1 = 1111 01102 + 1 = 1111 01112
Which is exactly what we had on the previous page!
More 2’s Complement Arithmetic
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 29
C. (continued)
This “proves”: addition works if the (Hex or) binary numbers
represent counting numbers or 2’s complement number!
left as an exercise: subtraction works, too!
Math works regardless of representation - this is why 2’s complement is used!
Why 2’s Complement
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 30
Encoding of Characters: (slides 30-33) want to represent “displayable” characters:
characters: { a, A, b, B , . . . , z, Z } decimal (10) digits: {0, 1, . . . , 8, 9 } punctuation: ! , . – ? / : ; math symbols: + * = ( – and / above) brackets: ( ) [ ] { } < > other symbols: @ # $ % ^ & \ | ~ “ ” etc. about 90 characters need to be represented
various encoding schemes have been used ASCII encoding – international standard American Standard Code for Information Interchange
Encoding of Characters
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 31
7-Bit ASCII Encoding• 7 bits to encode each character (128 codes)• often extended to 8-bit (byte) values by making most significant bit
(msb) = 0• Note: all codes are given as Hex values
• 00 – 1F non-displayable control char’s 00 NULL 07 BELL 08 backspace 09 tab others – often serve special purposes in communication
applications
Encoding of Characters
0A line feed 0C form feed 0D carriage return
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 32
7-Bit ASCII Encoding (continued) 20 = blank space “ ”; 21 = “!” ; 2E = “.” 30 – 39 decimal digit characters
30 = “0”, …, 39 = “9” 41 – 5A upper case letter characters
41 = “A”, …, 5A = “Z” 61 – 7A lower case letter characters
61 = “a”, …, 7A = “z”
Encoding of Characters (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 33
ASCII Example: “94.201 is FUN!”• hex encoding: 39 34 2E 32 30 31 20 69 73 20 46 55 4E 21• binary encoding: … 00110010 …. 01110011 … 00100001
hex is often used a shorthand for binary ASCII encoding is summarized in ASCII Table (on
web site)
Other Character Encoding Schemes: IBM standardized an 8-bit scheme (256 characters) as
defacto standard (PCs) Java: unicode – 16-bit scheme (65,536 characters)
• multi-lingual character sets
Encoding of Characters (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 34
fixed-width binary values are used to represent information in computers
the computer works with the binary representations (NOT the information!!)
the same binary value can be used to represent different information: Programmer must be aware!
8-bit example: 1111 00002 represents unsigned integer 24010
2s complement signed integer –1610
ASCII “ ” and others...
Data Representation Summary(Important Concepts)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 35
Manipulating Information in Computers: always operate on fixed-width binary representation arithmetic: add, subtract (slides 36-40) logic: and, or, xor (slides 41-42) shift and rotate (slides 43-45)
Data Manipulation(slides 35-45)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 36
1-bit operations – previous slides bitwise add/subtract values of width n to give result of width
n 8-bit Unsigned Integer Examples:
11710 = 0111 01012 13310 = 1000 01012
+ 9910 = 0110 00112 – 5110 = 0011 00112
21610 = 1101 10002 8210 = 0101 00102
do exactly the same thing for 2’s complement signed integers!
for 8-bit result: ignore any carry/borrow out/in @ msb (why?)
Addition and Subtraction
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 37
8-bit Signed Integer (2’s Complement) Examples: –110 = 1111 11112 3210 = 0010 00002
+ 110 = 0000 00012 – 6510 = 0100 00012
010 = 1 0000 00002 – 3310 = 1 1101 11112
= 0000 00002 = 1101 11112
in addition, ignore carry at msb for 8-bit result in subtraction, ignore borrow at msb for 8-bit result computers often implement subtraction using “negate and add”, i.e.
X – Y = X + (– Y) 3210 = 0010 00002
+ – 6510 = 1011 11112
– 3310 = 1101 11112
Addition and Subtraction
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 38
Problem: limited representation range due to fixed width
overflow occurs when the result of an operation is outside the range that can be represented
8-bit Unsigned Integer Example:
25510 = 1111 11112
+ 110 = 0000 00012
010 = 0000 00002 ?? 25610 ?? carry @ msb is important in this case! need 9 bits to represent 25610
used 8-bits: Overflow Occurred!
Overflow
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 39
But there is another interpretation, in 2’s complement:
–110 = 1111 11112
+ 110 = 0000 00012
010 = 1 0000 00002
= 0000 00002
The result is correct if the values are interpreted as signed integers! ( – 1 + 1 = 0 )
Note: Overflow depends on the interpretation of the values!
Overflow (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 40
Another example: unsigned signed
0111 11112 127 127
+ 0000 00012 + 1 + 1
1000 00002 128 – 128
OK Overflow
in this case there is overflow only in the signed representation
Overflow (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 41
AND, OR, XOR (exclusive or):
1-bit truth tables:
a b a AND b a OR b a XOR b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Logical Operations(slides 41-42)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 42
8-bit examples:
1011 0110 1011 0110
AND 1100 0011 OR 1100 0011
1000 0010 1111 0111
1011 0110
XOR 1100 0011
0111 0101
Logical Operations (contd)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 43
values can be manipulated by shifting left – towards msb right – towards lsb (least significant bit)
shifting example: value: 1001 1010 shifted left 4 bits: 1010 0000
question: should injected values be 0 or 1? shift left: always inject 0’s shift right:
logical: inject 0’s arithmetic: inject msb value
Shifting and Rotating(Slides 43-45)
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 44
Shift left 2 bits• value: 1001 1010
• shifted value: 0110 1000
Logical shift right 2 bits• value: 1001 1010
• shifted value: 0010 0110
Arithmetic shift right 2 bits• value: 1001 1010
• shifted value: 1110 0110
More Shifting
10-Sep-01 94.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 45
rotate: like shift, except bit that is shifted “out” of the value gets injected as the new bit
all bits in original value are saved left – towards msb right – towards lsb
rotating example: rotate right 3 bitsvalue: 1001 1110 rotated 1st bit: 0100 1111rotated 2nd bit: 1010 0111rotated 3rd bit: 1101 0011
Shifting and Rotating (contd)