730 likes | 873 Views
DATA REPRESENTATION. Digital computers store information in binary Binary allows two states * On yes true 1 * Off no false 0 Each digit in a binary number is called a bit 0 1 1 0 bit 0ff 0n 0n 0ff
E N D
DATA REPRESENTATION Digital computers store information in binary Binary allows two states * On yes true 1 * Off no false 0 Each digit in a binary number is called a bit 0 1 1 0 bit 0ff 0n 0n 0ff Binary number system == how numbers can be processed in binary CAP221
Binary Numbers • Digits are 1 and 0 • 1 = true • 0 = false • MSB – most significant bit • LSB – least significant bit • Bit numbering: CAP221
Number system • A number system of base (radix) r is a system that uses distinct symbol for r digits. • Numbers are represented by a string of digit symbols. CAP221
Number system • The base of a number is usually specified as a subscript, e.g.: • (01000011)2, • (71203)8, • (FF078ABC)16, ...etc. • Or a letter indicating the base (d for decimal, b for binary, o for octal and h for hexadecimal) is appended to the number, e.g.: • 01000011b, • 71203o, • FF078ABCh, ...etc. CAP221
Number system CAP221
Number system • The value of a digit depends not only on its value but also on its position within the number → gives the power of the radix by which it is multiplied. CAP221
Example 2 10 -1 -2 • 379.25= 3*10 + 7*10 + 9*10+2*10+5*10 n n-1 • (dndn-1...d0.f1f2…fm)r = dn* ( r ) + dn-1* ( r ) +….+ 0 -1 -2 -m d0* ( r ) + f1* ( r ) + f2* ( r ) +…+ fm* ( r ) Converting to decimal CAP221
Examples on converting from different bases to Decimal Convert the following to Decimal: • (1001001)2 • (203)8 • (FA07)16 Solution: • (1001001)2 = 1 + 0*21 + 0*22 + 1*23 + 0*24 + 0*25 + 1*26 = 73d • (203)8 = 3 + 0*81 + 2*82 = 131d • (FA07)16 = 7 + 0*161 + 10*162 + 15*163 = 64007d CAP221
Conversion from decimal • The conversion of a decimal integer into a base r is done by: • Whole numbers: divisions by r • Fractions: repeated multiplication by r. CAP221
Conversion from decimal To convert from decimal to any numbering system with base r : • The decimal number is divided by r, • Keeping the remainder aside, the result is further divided by r, and the new remainder is kept aside, • The new result is divided again by r, and so on till the result is less than r and this would be the last remainder, • The remainders make up the equivalent base-r number, with the last remainder being the most-significant digit and the first remainder being the least-significant digit. CAP221
Conversion to binaryWhole number • Ex.: (67)10 = (?)2 2 67 2 33 rem 1 LSB 2 16 rem 1 2 8 rem 0 2 4 rem 0 67d= 10000112 2 2 rem 0 2 1 rem 0 0 rem 1 MSB CAP221
Fraction Repeat multiplication by 2 until the fractional product is 0 Ex.: (0.3125)10 = (?)2 Carry 0.3125* 2 = 0.625 0 MSB 0.625* 2 = 1.25 1 (0.3125)10= (0.0101)2 0.25* 2 = 0.5 0 0.5* 2 = 1.00 1 LSB CAP221
Conversion to base r (50) 10 = (?)8 8 50 8 6 2 8 0 6 (50)10 = (62)8 Note: This technique can be used to convert from any radix to any other radix. CAP221
Octal and Hexadecimal number systems • Binary numbers are long. • On average, it takes about 3.3 times as many digits to represent a value in binary as it does to represent the same value in decimal. CAP221
Octal and Hexadecimal number systems If a base R1 is an integral power of d another base R2 i.e. R1= R2 → Group of d digits in base R2 maps directly into one digit in base R1 And each digit in base R1 maps directly into d digits in base R2. CAP221
Octal number systems 3 Base = 8 = 2 → d = 3 Digits: 0 → 7 Binary to Octal conversion: Group every 3 bits (starting from the right) Replace them by their corresponding octal digit Ex.: ( 0 1 0 1 1 0 1 0 1 )2 = ( 265 )8 ( 1 0 0 0 1 )2 = ( 21 )8 CAP221
Octal to binary conversion • Replace each digit by its 3-bit binary equivalent. • Ex.: (476)8 = (100111110)2 CAP221
Octal to binary conversion CAP221
Binary – Octal conversion table BinaryOctalBinaryOctal 000 0 001000 10 001 1 010000 20 010 2 011 3 100 4 101 5 110 6 111 7 CAP221
Hexadecimal number systems (Hex) 4 Base = 16 = 2 → d = 4 Digits: 0,1,2,…..,9,A,B,C,D,E,F Where A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 CAP221
Binary to Hex conversion Group each 4 bits and replace them by their corresponding hex digit. Ex.: (1 0 1 1 0 1 0 1 )2 = ( B5 )16 CAP221
Hex to binary conversion Replace each digit by their 4 bit binary equivalent Ex.: ( 1D9C )16 = (0001110110011100 )2 CAP221
Hex to binary conversion CAP221
Binary – Hexadecimal conversion table BinaryHexBinaryHex 0000 0 1000 8 0001 1 1001 9 0010 2 1010 A 0011 3 1011 B 0100 4 1100 C 0101 5 1101 D 0110 6 1110 E 0111 7 1111 F (00010100)2 → (14)16 → ( 20 )10 (12.8)16 →(00010010.1000)2 → (18.5)10 CAP221
Binary Arithmetic operations • Addition 0+0 = 0 0+1 = 1 1+0 = 1 1+1 = 10 (carry) CAP221
Binary Arithmetic operations • Subtraction 0 - 0 = 0 0 - 1 = 1 (after borrowing) 1 - 0 = 1 1 - 1 = 0 CAP221
Binary Arithmetic operations • Multiplication 0*0 = 0 0*1 = 0 1*0 = 0 1*1 = 1 CAP221
ADDITION • Like decimal numbers, two numbers can be added by adding each pair of digits together with carry propagation. CAP221
Example Carries CAP221
SUBTRACTION • Two numbers can be subtracted by subtracting each pair of digits together with borrowing, where needed. CAP221
SUBTRACTION CAP221
Representation of signed binary numbers • Given a fixed number of bit positions (n), we n • can represent 2 patterns: (00..0)2 through (11..1)2. • Using unsigned numbers we can represent n 0 → 2 -1 n • Using signed numbers → 2 positive & negative patterns CAP221
Complementary notations • The most commonly used way of representing signed numbers. It greatly simplifies all arithmetic operations. • Positive number is represented as it is (like an unsigned positive number). • Negative numbers are represented by the complement of unsigned number. CAP221
Complementary notations • The value used for complementation is a power of the base or less one than a power of the base. • For binary numbers: we have 2’s complement and 1’s complement. • For decimals, we have 10’s complement and 9’s complement. • For hex. Numbers we have 16’s complement and F’s (15’s) complement. CAP221
One’s complement • Is obtained by subtracting the number from (11..1)2 (n 1’s) → reversing each bit: changing every 1 to 0 and every 0 to 1. • Ex.: represent +(14d), -(14d) in 8-bit binary number • (+14)10 = ( 00001110)2 = (00001110)1s • (-14)10 = -( 00001110)2 = (11110001)1s CAP221
For 8-bits number system: • Largest positive number: 0 1111111 = +(127)10 • Largest negative number: 1 0000000 = - (127)10 • Zeroes: 0 0000000 1 1111111 • Range: -(127)10 to +(127)10 m-1 m-1 - (2 -1) to + 2 -1 • The most significant bit represents the sign: 0 = +ve ; 1 = -ve. CAP221
2’s complement • Take the 1’s complement and add 1 → invert all the bits and add 1. • Ex.: 2’s complement of (01110100)2: 1’s complement = (10001011)2 add 1 → 2’s complement = (10001100)2 CAP221
2’s complement • 2’s complement of (00000000)2: 1’s complement = (11111111)2 add 1 → (100000000)2 • 2’s complement of n-bit number include only the rightmost n bits: 2’s complement of (00000000)2 = (00000000)2 CAP221
2’s complement - For 8-bit binary numbers 2’s complement of (5)d = (00000101)2: 1’s complement = (11111010)2 add 1→ (11111011)2 510 + (-5)10 → 000001012 + 111110112 = 1000000002 Carry 2’s complement of any N integer represents –N: N + (-N) = 0 CAP221
2’s complement • To find the 2’s complement of a binary number, proceeding from right to left, leave all bits unchanged up to and including the first `1`. Reverse all the remaining bits. • Range of signed value that can be represented in 2’s complemented notation is m-1 m-1 - 2 to 2 -1 CAP221
For 8-bits number system: • Largest positive number: 0 1111111 = +(127)10 • Largest negative number: 1 0000000 = - (128)10 • Zero: 0 0000000 • Range: -(128)10 to +(127)10 • The most significant bit represents the sign: • 0 = +ve ; 1 = -ve. CAP221
2’s complement in hexadecimal • Each group of four bits corresponds to a single hexadecimal digit. • Subtract each hexadecimal digit from F and then add 1 (equivalent of taking the 16’s complement). CAP221
Ex.: 2’s complement of (3A6E)16 1’s complement: FFFF - 3A6E C591 2’s complement: (C592)16 CAP221
Comparing 2’s complement numbers Check the signs: If they differ, they determine the order If they are the same, the order of the numbers is the same as that of their representations. CAP221
Ex.: (01001100)2 and (10100101)2 Different signs → the 1st # is +ve the 2nd # is –ve → 1st # > 2nd # CAP221
(10110010)2 and (10111001)2 • Both numbers are negative: 1st # < 2nd # CAP221
Signed binary arithmetic Overflow • Signed binary numbers are of a fixed range. • If the result of addition/subtraction goes beyond this range, overflow occurs. • Two conditions under which overflow can occur are: (i) positive add positive gives negative (ii) negative add negative gives positive CAP221
2s complement addition Algorithm: • Perform binary addition on the two numbers. • Ignore the carry out of the MSB. • Check for overflow: Overflow occurs if the carriers into and out of the MSB are different. CAP221