Download presentation
Presentation is loading. Please wait.
Published byProsper Strickland Modified over 9 years ago
1
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20011 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
2
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20012 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: d 3 d 2 d 1 d 0 “value” of a digit depends on value of symbol and position of digit in string value of digit i = (value of symbol i ) n i “value” of number is sum of value of digits Base-n Number Systems
3
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20013 (nothing new here!) symbols = { 0, 1, 2, 3, , 9} – usual meaning 4-digit example: d 3 d 2 d 1 d 0 = 1436 = d 3 10 3 + d 2 10 2 + d 1 10 1 + d 0 10 0 = 1 10 3 + 4 10 2 + 3 10 1 + 6 10 0 = 1 1000 + 4 100 + 3 10 + 6 1 = 1000 + 400 + 30 + 6 = 1436 10 Base-10 Example
4
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20014 set of symbols = { 0, 1 } – usual meaning 4-digit number example: 1011 2 = 1 2 3 + 0 2 2 + 1 2 1 + 1 2 0 = 1 8 + 0 4 + 1 2 + 1 1 = 8 + 0 + 2 + 1 = 11 10 Base-2 (binary) Number System
5
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20015 Binary Decimal Binary Decimal 0000 0 1000 8 00011 1001 9 00102 101010 00113 101111 01004 110012 01015 110113 01106 111014 01117 111115 Binary – Decimal Conversion Table Memorize these!
6
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20016 What about the range of representation? n-bit values can represent (at most) 2 n different counting numbers (why?) if start representation at 0, then largest value represented is 2 n – 1 (why?) recall 4-bit example: ( 2 4 = 16 ) –range = 0.. 15 Range of Representation
7
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20017 Some Observations: binary representation of counting numbers often requires lots of bits –example: 601 10 = 1001011001 2 (10 bits!) converting binary decimal is not trivial binary can be awkward and error prone for use by people! More about Binary
8
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20018 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
9
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 20019 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
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200110 16-bits: 1000101001101110 2 (= 35,438 10 ) 1. group in 4’s 1000 1010 0110 1110 2. replace with hex 8 A 6 E 16 10-bits: 1001011001 2 (= 601 10 ) 1. group in 4’s 0010 0101 1001 2. replace with hex 2 5 9 16 Binary => Hex Examples
11
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200111 Binary Decimal Hex BinaryDecimal Hex 0000 0 0 1000 8 8 0001 1 1 1001 9 9 0010 2 2 1010 10 A 00113 3 1011 11 B 01004 4 1100 12 C 01015 5 1101 13 D 01106 6 1110 14 E 01117 7 1111 15 F Binary / Decimal / Hexadecimal Memorize these!
12
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200112 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
13
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200113 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 10000001 2 magnitude = 1 sign bit = 1 negative number Thus this represents value – 1 Signed Magnitude Representation
14
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200114 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)
15
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200115 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
16
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200116 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
17
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200117 Assume we have n bits available. use half of binary values for negative values (i.e. 2 n–1 values), the rest for non-negative encode positive values (and 0) the same way as counting numbers range 0.. 2 n – 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
18
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200118 1. 8-bit 2’s complement encoding of –1 +1 10 0000 0001 complement: 1111 1110 add 1 + 1 so – 1 10 =1111 1111 the largest binary representation is for the smallest negative number! Integer Encoding Examples
19
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200119 2. 8-bit 2’s complement encoding of –128 +128 10 1000 0000 complement: 0111 1111 add 1 + 1 –128 10 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? –128 10 = +128 10 ? No, as +128 10 cannot be represented in 8-bit 2’s complement (see next page!) Integer Encoding Examples (contd)
20
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200120 range of numbers that can be represented is: – 2 n – 1, …, – 1, 0, …, 2 n – 1 – 1 e.g. for 8-bit 2’s complement encodings: n = 8: 2 8 = 256, 2 7 = 128 range: – 2 n – 1, …, 2 n – 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
21
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200121 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
22
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200122 A. Negating a negated number should give the original number: with 2’s complement does – (– x) = x? Example 1: (8 bits) – 1 10 1111 1111 complement 0000 0000 add 1 + 1 +1 10 0000 0001 – ( – 1 ) = 1it works! 2’s Complement (contd)
23
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200123 A. (continued) Negating a negated number should give the original number: Special Case! Example 2: (8 bits) – 128 10 1000 0000 complement 0111 1111 add 1 + 1 – 128 10 1000 0000(we saw this already!) Should be +128 10 (which cannot be represented in 8 bits 2’s complement) What happened? Overflow (more later!) 2’s Complement (contd)
24
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200124 B. How many representations are there for 0 in 2’s complement? 0 10 0000 0000 complement 1111 1111 add 1 + 1 (ignore carry out of “1”) 0 10 0000 0000 2’s Complement (contd)
25
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200125 C. (Example) Add: 18 10 + 75 10 (should be 93 10 ) Convert to Hex and Binary: 18 10 = 1*16 + 2 = 12h = 0001 0010 2 75 10 = 4*16 + 11 = 4Bh = 0100 1011 2 Add in Hex and Convert back to Decimal: 12h + 4Bh = 5Dh = 5*16 + 13 = 93 10 Add in Binary and Convert back to Decimal: 0001 0010 2 + 0100 1011 2 = 0101 1101 2 = 1*64 + 1*16 + 1*8 + 1*4 + 1*1 = 93 10 BinaryAddition worked! Binary Math Example
26
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200126 C. (continued) What if the binary numbers on the previous page were in 2’s complement representation? 0001 0010 2 0100 1011 2 0101 1101 2 No difference as all these numbers are positive (most significant bit is “0”) 2’s Complement Math (contd)
27
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200127 Add: 10 10 + 237 10 (should be 247 10 ) Convert to Hex and Binary: 10 10 = 0*16 + 10 = 0Ah = 0000 1010 2 237 10 = 14*16 + 13 = EDh = 1110 1101 2 Add in Hex and Convert back to Decimal: 0Ah + EDh = F7h = 15*16 + 7 = 247 10 Add in Binary and Convert back to Decimal: 0000 1010 2 + 1110 1101 2 = 1111 0111 2 = 1*128 + 1*64 + 1*32 + 1*16 + 1*4 + 1*2 + 1*1 = 247 10 As expected, BUT... More Binary Arithmetic
28
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200128 C. (continued) What if these binary numbers were in 2’s complement representation? 0000 1010 2 positive number so no change = 10 10 1110 1101 2 negative number so complement and add 1 = – (0001 0010 2 + 1) = – 0001 0011 2 = – (1*16 + 1*2 + 1*1) 10 = – 19 10 Thus we would want the answer to be: 10 10 + – 19 10 = – 9 10 converting – 9 10 to 2’s complement gives: 0000 1001 2 + 1 = 1111 0110 2 + 1 = 1111 0111 2 Which is exactly what we had on the previous page! More 2’s Complement Arithmetic
29
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200129 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
30
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200130 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
31
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200131 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 – 1Fnon-displayable control char’s 00NULL 07BELL 08backspace 09tab others – often serve special purposes in communication applications Encoding of Characters 0Aline feed 0Cform feed 0Dcarriage return
32
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200132 7-Bit ASCII Encoding (continued) 20 = blank space “ ”; 21 = “!” ; 2E = “.” 30 – 39decimal digit characters 30 = “0”, …, 39 = “9” 41 – 5Aupper case letter characters 41 = “A”, …, 5A = “Z” 61 – 7Alower case letter characters 61 = “a”, …, 7A = “z” Encoding of Characters (contd)
33
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200133 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)
34
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200134 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 0000 2 represents unsigned integer 240 10 2s complement signed integer –16 10 ASCII “ ” and others... Data Representation Summary (Important Concepts)
35
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200135 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)
36
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200136 1-bit operations – previous slides bitwise add/subtract values of width n to give result of width n 8-bit Unsigned Integer Examples: 117 10 = 0111 0101 2 133 10 = 1000 0101 2 + 99 10 = 0110 0011 2 – 51 10 = 0011 0011 2 216 10 = 1101 1000 2 82 10 = 0101 0010 2 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
37
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200137 8-bit Signed Integer (2’s Complement) Examples: –1 10 = 1111 1111 2 32 10 = 0010 0000 2 + 1 10 = 0000 0001 2 – 65 10 = 0100 0001 2 0 10 = 1 0000 0000 2 – 33 10 = 1 1101 1111 2 = 0000 0000 2 = 1101 1111 2 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) 32 10 = 0010 0000 2 + – 65 10 = 1011 1111 2 – 33 10 = 1101 1111 2 Addition and Subtraction
38
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200138 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: 255 10 = 1111 1111 2 + 1 10 = 0000 0001 2 0 10 = 0000 0000 2 ?? 256 10 ?? carry @ msb is important in this case! need 9 bits to represent 256 10 used 8-bits: Overflow Occurred! Overflow
39
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200139 But there is another interpretation, in 2’s complement: –1 10 = 1111 1111 2 + 1 10 = 0000 0001 2 0 10 = 1 0000 0000 2 = 0000 0000 2 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)
40
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200140 Another example: unsigned signed 0111 1111 2 127127 +0000 0001 2 + 1+ 1 1000 0000 2 128 – 128 OK Overflow in this case there is overflow only in the signed representation Overflow (contd)
41
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200141 AND, OR, XOR (exclusive or): 1-bit truth tables: ab a AND b a OR b a XOR b 00000 01011 10011 11110 Logical Operations (slides 41-42)
42
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200142 8-bit examples: 1011 0110 1011 0110 AND1100 0011 OR1100 0011 1000 0010 1111 0111 1011 0110 XOR1100 0011 0111 0101 Logical Operations (contd)
43
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200143 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)
44
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200144 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
45
10-Sep-0194.201 - Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 200145 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 bits value:1001 1110 rotated 1 st bit:0100 1111 rotated 2 nd bit:1010 0111 rotated 3 rd bit:1101 0011 Shifting and Rotating (contd)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.