170 likes | 204 Views
Computer Representation of Information. Outline. Representation of Characters Mathematical operations on numbers (addition) Signed integer representation Sign-magnitude notation Two’s complement notation Binary Notations Exercises Real Numbers Representation. Representation of Characters.
E N D
Outline • Representation of Characters • Mathematical operations on numbers (addition) • Signed integer representation • Sign-magnitude notation • Two’s complement notation • Binary Notations Exercises • Real Numbers Representation CSCE 106
Representation of Characters • A unique binary pattern/value is used to represent each of the printable characters on your keyboard (e.g. A, a, 4, *, [,’, etc. …), as well as the special control/unprintable characters (e.g. carriage return, line feed, tab, space, etc. …). • A byte (8 bits) gives you the opportunity to have 28 (256) unique representations/patterns/values. • There are standard character coding schemes to ease interchange of information, e.g. ASCII. • The code is designed in such a way not only to preserve uniqueness, but also to keep the order which is often needed in manipulating characters/text, e.g. sorting. • The value/pattern used to represent “A” is less by one than the value used to represent “B”, … etc. CSCE 106
Mathematical Operations on Numbers (Addition) • Most of the mathematical operations on numbers from different number systems are conceptually identical to the decimal arithmetic you are used to. • To add two numbers you do the following: Start at the rightmost digit. While there are more digits: Add the current digit of each operand. If the sum is less than the base/radix then record that sum, otherwise record the difference between the sum and the base/radix, and add one to the next digit of operand 1. CSCE 106
Mathematical Operations on Numbers (Addition) (cont’d) • Decimal numbers: 1610 + 1510 ------ 3110 Start by adding 6 + 5. The sum (11) is not less than the base (10), so (11-10) record 1, and carry 1 by adding it to the first digit of 16, giving 2, then add the next digit from each operand (2+1) to give 3. • Octal numbers: 168 + 158 ------ 338 Start by adding 6 + 5. The sum (1110) is not less than the base (8), so (11-8) record 3, and carry 1 by adding it to the first digit of 16, giving 2, then add the next digit from each operand (2+1) to give 3. • The above discussions can be generalized to any base. CSCE 106
Signed Integers • So far, we were representing unsigned integers in binary. • We need to have a way for representing the sign of the number (positive or negative). • We need to have a sign bit, or figure out some other way, so as to be able to represent negative values as well as positive ones. • We will study two notations for representing signed integers: • sign-magnitude notation, and • two’s complement notation. CSCE 106
Sign-Magnitude Notation • It leaves the left most bit for the sign (sign bit), and uses the rest of the bits (m - 1) to represent the integer. • 0 in the sign bit is used to represent positive values, and 1 is used to represent negative values. • Therefore, the range of integer values i which can be represented with m bits is: -(2m-1 – 1) <= i <= +(2m-1 – 1) • Using the sign–magnitude notation (in a byte): • +2910 is represented as 0001 1101 • -2910 is represented as 1001 1101 CSCE 106
Problems with Sign-Magnitude Notation • You might find this technique simple, natural, and straight forward, as it closely resembles the way you are used to writing numbers. • However this notation creates mainly two problems for the computers: • 0 representation (00000000 is not 10000000). • Addition of mixed sign numbers (10000101 (–510) and 00000001 doesn’t result in –4). • Thus the sign-magnitude notation is not used in computers. CSCE 106
Two’s Complement Notation • Two’s complement of an m-bit number N = (Bitwise/one’s complement of N)+ 1 • Examples of 8-bit numbers and their 2’s complement representation: • +1 = 000000012 • –1 = 11111110 + 1 = 11111111 • +29 = 000111012 • –29 = 11100010 + 1 = 11100011 (2’s complement of 111000112 = 000111002 + 1 = 000111012 (29)) CSCE 106
Two’s Complement Notation (cont’d) • For an m-bit signed integer in two’s complement notation, the range of integer values is from –2m–1 to 2m–1 – 1. • When m = 8, the range is from –128 to 127. • When m = 16, the range is from –32768 to 32767. • Two’s complement notation overcomes the problems of sign-magnitude notation. • We have only one representation for the 0. • Addition of mixed sign integers give correct results (with no need for extra logic). CSCE 106
Binary Notations Exercises Representations of integers in a byte (8 bits) using different binary notations: IntegerUnsignedSign-magnitude2's-complement 34 001000102 001000102 001000102 -23 Not Possible100101112 111010012 -128 Not Possible Not Possible 100000002 310 Not Possible Not Possible Not Possible CSCE 106
Exercises (cont’d) 11 11 Carry 000110012 110110112 ------------- 111101002 • Add (-37) to (25) using 8-bit two’s complement representation. Ans: 2510 -3710 ------ -1210 + + CSCE 106
Exercises (cont’d) 1111111 Carry 111011112 110110012 ------------- 110010002 Final carry overflows 8 bits. • Subtract (39) from (-17) using 8-bit two’s complement representation. Ans: -1710 3910 ------ -5610 - + CSCE 106
Real Numbers Representation • Real or Floating-Point Numbers, e.g. 22.625 • In binary decimal point binary point CSCE 106
Real Numbers Representation (cont’d) exponent mantissa • Scientific notation: 2.2625 x 101 • Binary scientific notation: 1.0110101 x 24 • Hence it consists of a sign bit, a mantissa field, and an exponent field. exponent mantissa CSCE 106
Next lecture will be about Problem Solving Methods CSCE 106