Representing Numbers Positive Integers Negative Integers Arithmetic on
Representing Numbers Positive Integers Negative Integers Arithmetic on Integers Real Numbers Characters
Positive Integers Just as decimal numbers use 10 digits, 0 through 9, computers use 2 bits, 0 through 1 The place of the digit tells its value 27 111 Similarly 111 2 *101 +7 * 100 1*102 +1 * 101 + 1 * 100 1*22 +1 * 21 + 1 * 20 = 7 decimal Notation 1112 = 710 Converting decimal numbers to binary numbers • Repeatedly divide decimal number by 2 till quotient is either 0 or 1 • Place remainder of 1 st division in right most place, of 2 nd in 2 nd rightmost and so on
Converting Numbers Convert 2710 quotient remainder 13 1 6 1 3 0 1 1 Quotient is 1 therefore stop Binary Number 1 11 011 11011 Convert 110112 to decimal 1*24 +1 * 23 + 0* 22 + 1*21 + 1 * 20 = 16 +8+0+2+1 = 2710 The number of bits required to represent a number is rather large. Use Octal numbers base 8 : use 0 through 8. Same conversion algos from decimal except use 8 instead of 2. Binary to Octal by groups of three, 11011 = (011) = 338 Use hexadecimal numbers bade 16: the 10 decimal digits and A, B, C, D, E, F for 10, 11, 12, 13, 14 , 15: similar algos except use 16 Binary to hex by groups of four, Binary 11011 = (0001) (1011) = 1 B 16
Operations Add Subtract 0101 1110 RESULT 10011 Result Multiply the numbers 0000 0101 Result 1110 0101 1001 Divide 1110 by 0101 = 1110/101 10 is quotient and 100 is remainder Division in computers: by repeated subtraction 1000110
Negative Integers Assume a computer that can represent a number in n bits It can represent 0 through 2 n – 1 numbers If a negative number is to be represented then Sign Magnitude Representation Top order bit indicates sign, 0 indicates + and 1 indicates – Now can represent - (2 n-1 – 1) to + (2 n-1 – 1) Advantages • Taking a negative of a number is easy: switch top order bit • Determining sign of number is easy: check top order bit • Easy to perform multiplication and division on signed numbers: the sign bits tell the sign of the result Disadvantages • Addition and subtraction is difficult with signed numbers • Two zeroes: +0 and -0
Operations Multiply two signed numbers: multiply magnitudes and determine sign 000111 100101 +7 -5 Multiplication of magnitudes gives 100011 Sign should be negative, giving 1100011 Add two signed numbers +10 00001010 -4 10000100 Result 10001110 that is -14 but the answer should be +6
Radix Complement Given a negative number in radix r, take (r-1)’s complement by subtracting each digit from (r-1) and adding one to the resulting number 735 – 78 = 735 + (1000 -78) – 1000 = 735 + 922 – 1000 How do you get 922? 922 = 921+1 735 – 78 = 735 + 921+1 -1000 But 921 = 999 -78 So take the 9’s complement of 78 to get 921 9’s complement of 8 is 9 -8 = 1 7 is 9 -7 = 2 0 is 9 -0 = 9 Increment 9’s complement by 1 to get 922 735 +922 -1000 = 1657 -1000 = 657 In other words, discarding the highest order digit amounts to subtracting 1000.
Negative Numbers: 2 s Complement To represent a negative number, invert each bit and add 1, discard any additional (overflow) bits to left. Consider 8 -bit arithmetic +12 00001100 Invert Add 11110011 1 Result 11110100 Add +12 -12 00001100 11110100 Result 0000 by dropping the additional 9 th bit -12 in 2 s complement only one zero: represent –(2 n-1) to +(2 n-1 -1) high bits gives the sign, negation of negation gives original number 2 s complement arithmetic is easier to implement and is therefore used.
2’s Complement Addition and Subtraction Addition +12 = 00001100 + 00001100 = 00011000 Subtraction +12 -12 = 00001100 + 11110011 1111 + 00000001= 10000 Discard top most bit to get 0000
2’s Complement Multiplication: works for both positive and negative multipliers and multiplicands • Sign extend the multiplier and multiplicand to twice the number of bits • multiply • discard extra bits +12 * +12 = 00001100 * 00001100 000000000000000000001100 10010000 take bottom 8 bits 128+16 = 144
2’s Complement Multiplication Do +3 * - 3 in 4 – bits 2’s complement for -3 is ( flip 0011 +1) = 1100 +1 = 1101 So, we get 0011 * 1101 Sign extension 00000011 11111101 Multiplication is done by the Booth’s algorithm. The implementation of the algorithm will be in Project 2 00000011 00000011 00000011 10110111 Take lower order 4 bits 0111 This is 2’s complement of 9 i. e. -9
2’s Complement Division Same principle as in decimal division Remainder takes the sign of the dividend Quotient is negative if the signs of dividend and divider are different 7 / 3 = 2 remainder = 1 7 / - 3 = - 2 remainder = 1 - 7 / 3 = - 2 remainder = - 1 - 7 / - 3 = 2 remainder = - 1 The division algorithm will be part of project 2 This implies If dividend is negative, then two's complement must be applied to the remainder at the end. If the dividend and the divisor have different signs, then the quotient must be negated with 2's complement operation at the end.
Overflow • When two n-digit numbers are added and the sum is n+1 digits, we say that there is an overflow. • Overflow is a problem because the n+1 digit result cannot be stored in the standard n-digit register. – One bit, the most significant bit, will be lost. – The number becomes unusable. • When two un-signed numbers are added, an overflow occurs when a carry out of the most significant bit is produced. • When two signed numbers of the same sign are added, an overflow occurs when the result has a different sign. • When two signed numbers with different signs are added, an overflow cannot occur.
Overflow Examples Un-Signed Numbers 0011 + 0100 0111 1100 + 0101 10001 Signed Numbers No Overflow 0101 + 0100 1001 Overflow 1001 + 1010 10011 Overflow 14
Overflow Detection • Detecting an overflow depends on the representation of the data. – If the data is unsigned, we only need to check for a carry out of the most significant bit. – If the data is signed, an overflow can be detected by watching the carry into the sign bit and the carry out of it. • If the two are not the same, an overflow has occurred. 15
Floating Point Numbers, IEEE 754 For numbers with fractional values or those that are too large for the word length of the machine 2 *10 -2 = 0. 02 Analogously mantissa * 2 exponent Single Precision numbers (32 bits) Sign Exponent Mantissa 1 8 bits 23 bits Double Precision Sign Exponent Mantissa 1 11 bits 52 bits
Floating Point Numbers, IEEE 754 Mantissa is a sign-magnitude representation of a number, Magnitude is of the form 1. fraction Represent 6. 25 = 1*22 + 1*21 + 0*20 +0*2 -1 +1* 2 -2 = 4+2 + ¼ = 6. 25 For whole number part, divide by 2 as before. For the decimal part, repeatedly multiply by 2 till 0 is reached, the whole number of the result is the next closest to the binary point, . As before 6 yields 110 0. 25 * 2 = 0. 5 *2 = 1. 0 0 *2 = We get 110. 010 etc. pad with zeroes till mantissa filled (1. 1001 0……. * 22) to yield a 23 bit mantissa 10010000000000 Sign Exponent Mantissa 0 ? ? ? 10010000000000
Floating Point Numbers, IEEE 754 Computing the exponent part: use a bias to add to the basic exponent number Due to hardware optimization reasons Bias of 127 for single precision and 1023 for double precision We have (1. 1001 0……. * 22) Add 127 to the exponent 2 = 129 = 10000001 Sign Exponent Mantissa 0 10000001 10010000000000
Floating Point Arithmetic Addition • If exponents are different, then make them the same by shifting mantissa and adjusting the exponent • while shifting mantissa to the right, the hidden bit becomes most significant bit of mantissa • Add the numbers including the hidden bit • Normalize the result Subtraction • Compare magnitudes including the hidden bit • Change sign bit if order of operands is changed • Align exponents as before • Use sign magnitude subtraction (NOT 2’s complement) • Normalize the number
Representing Characters
ASCII Character Codes (1) The ASCII Character set: characters 0 – 31.
ASCII Character Codes (2) The ASCII Character set: characters 32 – 127.
Extended Binary Coded Decimal Inter. Change, EBCDIC Existed in at least six different versions Basic differences with ASCII EBCDIC ASCII Letters before numbers Numbers before letters Lowercase before uppercase Uppercase before lowercase Lacks symbols like {} Large number of unassigned codes (41 between A and Z) All assigned 26 characters between A and Z
Universal Coded character Set, Unicode can be implemented by different character sets. The most commonly used encodings are UTF-8 and UTF-16: Unicode Transformation Format UTF-8 • A character can be from 1 to 4 bytes long. • Can represent any character in the Unicode standard. • Backwards compatible with ASCII, first 128 characters are identical to ASCII • Preferred encoding for e-mail and web pages First 128 characters (ASCII) 1 byte Latin, Greek, Armenian, Hebrew, Arabic, Syriac, Thaana and N'Ko alphabets, as well as Combining Diacritical Marks. 2 bytes Basic Multilingual Plane including Chines, Japanese, Korean 3 bytes Historic scripis, mathematical sysmbols, emoji 4 bytes
UTF-16 encodes all 1, 112, 064 valid code points of Unicode • Used in major operating systems and environments, like Microsoft Windows, Javascript, and. NET. • Rarely used for files on Unix/Linux or mac. OS • Web uses UTF-8 • Recommendation by Web Hypertext Application Technology Working Group u not to use UTF-16 for browser apps for security reasons The first 128 characters of UTF-16 are same as of UTF-8 A few examples: "A" in ASCII is hex 0 x 41; in UTF-8 it is also 0 x 41; in UTF-16 it is 0 x 0041 "À" in Latin-1 is 0 x. C 0; in UTF-8 it is 0 x. C 3 0 x 80; in UTF-16 it is 0 x 00 C 0 The Tibetan letter �in UTF-8 is 0 x. E 0 0 x. BD 0 x. A 8; it UTF-16 it is 0 x 0 F 68
- Slides: 25