Data Representation and Computer Arithmetic Computer Systems Data Representation and Computer Arithmetic
Data Representation Binary codes used to represent numbers Conversion between number systems Binary decimal Hexadecimal decimal Between arbitrary number systems Representation of positive/negative numbers Floating point representation
Bit, Byte and Word Bit is the smallest quantity that can be handled by computer: 1 or 0. Byte is a group of 8 bits. Word is the basic unit of data that can be operated by computer: e.g., 16 bits. 11001010 00001101 Bit Byte Word Some architectures have 8, 32, or 64-bit words
Content of Word Bit Pattern May represent many things Actual meaning of a particular bit pattern is given by the programmer Computer itself cannot determine the meaning of the word Classic question: Can you tell the meaning of a word picked randomly from the memory?
Instruction: (op-code) A single word defines an action that is to be performed by CPU Numeric Quantity Character: A to Z, a to z, 0 to 9, *, -, +, etc. ASCII Code: - Representation of a character by 7 bits. - The ASCII for W is 101 01112, or 5716
ASCII Code
Pixel (Picture Element) The smallest unit to construct a picture 1bit/pixel for black-and-white pictures, >1 bits/pixel for gray scale or color pictures, e.g. 24bits/pixel. A letter is represented by a group of pixels on computer screen. Pixel (1)
Positional Notation Weight is associated with the location within a number. The 9 in 95 has weight 10 A number N in base b is represented by
Example:
Number Bases Decimal (b=10): {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Binary (b=2): {0, 1} Octal (b=8): {0, 1, 2, 3, 4, 5, 6, 7} Hexadecimal (b=16): {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
How many binary bits are needed to represent an n-digits decimal? The value of an m-bit binary is up to The value of an n-digit decimal is up to so OR
Questions How many bits are needed to represent an n-digits octal? How many bits are needed to represent an n-digits hexadecimal?
Conversion of Integer - Decimal to Binary Divide a decimal successively by 2, record the remainder, until the result of the division is 0. 67/2 = 33 Rem.=1 33/2 = 16 Rem.=1 16/2 = 8 Rem.=0 8/2 = 4 Rem.=0 4/2 = 2 Rem.=0 2/2 = 1 Rem.=0 1/2 = 0 Rem.=1
Conversion of Integer - Decimal to Hexadecimal Divide a decimal successively by 16, record the remainder, until the result is 0 432/16 = 27 Rem.=0 27/16 = 1 Rem.=B 1/16 = 0 Rem.=1
Conversion of integer - Binary to Decimal Conversion of integer - Hexadecimal to Decimal
Conversion of integer - Hexadecimal Binary 0 1 0 1 0 1 1 1 5 7 1011 2 B 7 0010 0111 Conversion of integer: - Octal Binary 0 1 0 1 0 1 1 1 2 7 1 2 5 7 010 111 101
Conversion of Fraction - Decimal to Binary The fraction is repeatedly multiplied by 2, the integer part is stripped and recorded. 0.687510 = 0.10112 Question: Convert 0.625? 0.110? 0.687510 * 2 =1.375 1 0.37510 * 2 = 0.75 0 0.7510 * 2 = 1.5 1 0.510 * 2 = 1.0 1 0.010 * 2 = 0.0 end
Conversion of Fraction - Binary Hexadecimal 0.1010110012 = 0.1010 1100 10002 = 0.AC816 0.7BC16 = 0.0111 1011 11002 = 0.01111011112 Convert 0.1100101011 Convert 0.6D5?
Binary Coded Decimal (BCD) A decimal digit is coded into 4 bits 1 byte can store 2 digits Example: 1942 is encoded as 0001 1001 0100 0010 (2 bytes) Disadvantages Complex arithmetic Inefficient use of storage Advantage: Can represent “real” number Decimal BCD 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001
Binary Addition Binary Subtraction ? 0 0 1 1 0 1 1 1 + 0 1 0 1 0 1 1 0 0 0 1 1 0 1 1 1 + 0 1 0 1 0 1 1 0 1 1 1 1 1 Carry 1 0 0 0 1 1 0 1 Binary Subtraction ? use 2’s complementary arithmetic (later … )
Signed Integer Representations Sign-and-Magnitude 1’s Complement 2’s Complement
Sign-and-Magnitude Representation Use the most significant bit (MSB) to indicate the sign of the number. The MSB is 0 for positive, 1 for negative. 8 bits represent -12710 to 12710 Examples: 00101100 for +4410 10101100 for -4410 Problem: 00000000 = +0, 10000000 = -0
1’s Complement Just flip the bits! The MSB is 0 for positive, 1 for negative. 8 bits represent -12710 to 12710 Examples: 00101100 for +4410 11010011 for -4410 Problem: 00000000 = +0, 11111111 = -0
2’s Complement The 2’s complement for N is Example: Using 5 bits (n = 5), if N = 7, then 12 -7 5 0 1 1 0 0 + 1 1 0 0 1 1) 0 0 1 0 1 2’s complement = 1’s complement + 1 7 0 0 1 1 1 1’s complement 1 1 0 0 0 + 1 2’s complement 1 1 0 0 1 = -7
2’s Complement (con.) To form the two’s complement of a number, simply invert the bits (1 to0, 0 to 1), and add 1 1’s complement + 1 1310 = 0 0 0 0 1 1 0 1 -1310 = 1 1 1 1 0 0 1 0 + 1 = 1 1 1 1 0 0 1 1 2’s comp. -34?
Properties of 2’s Complement One unique 0 MSB = 0, positive number MSB = 1, negative number The range is For 5 bits, the range is -1610 (10000) to +1510 (01111) The 2’s complement of the complement of X is X itself (Prove: )
2’s Complementary Arithmetic Subtraction is performed using addition A - B = A + (-B) X = 910 = 010012, Y = 610 = 001102 -X = -910 = 101112, -Y = -610 = 110102 X -Y 310 0 1 0 0 1 + 1 1 0 1 0 1) 0 0 0 1 1 - X - Y -1510 1 0 1 1 1 + 1 1 0 1 0 1) 1 0 0 0 1
Binary Adder/Subtractor
Arithmetic Overflow The overflow happens when positive + positive negative negative + negative positive If are MSBs, then the overflow bit v is expressed as 12 +13 25 0 1 1 0 0 + 0 1 1 0 1 1 1 0 0 1 -12 -13 -25 1 0 1 0 0 + 1 0 0 1 1 1) 0 0 1 1 1
Arithmetic Overflow In practice, Case I: A and B are + an-1 = __ bn-1 = __ cn = __ cn-1 = __ V = _____ Case II: A and B are – Most significant stage of full adder
Fixed-point Arithmetic 3.62510 0011.10102 00111010 + 6.510 0110.10002 + 01101000 10.12510 10100010 The fraction point is added 10100010 -> 1010.00102 What if we want to represent ? We need at least m > 3.3*n = 3.3*20 = 66 bits !
Floating Point Numbers Scientific Notation a is mantissa, r is radix, e is exponent is for binary
IEEE Floating Point Format S: Sign bit, 1 bit E: Exponent, 8 bits B: Bias, 8 bits, 12710=0111 1111 F: mantissa
IEEE Floating Point - Example Normalization, e.g., Use sign and magnitude representation for signed mantissa. Use biased exponent, e.g. in excess 127 If we have 8 bits for exponent, to represent -5, we add 12710 (0111 1111) to -5, the result is 12210=0111 10102.
IEEE Floating Point - Example Represent -2345.125 in 32 bits ? -2345.125 = -100100101001.0012 1) Normalization: 2) Negative mantissa, so S = 1 3) Exponent: E = 11 + 127 = 13810 = 100010102 4) Mantissa F = 00100101001 0010…000 1 10001010 00100101001001000…0 1.510 in IEEE format?
IEEE Floating Point Format – Special Cases Zero: exponent and fraction all 0s Denormalized: exponent all 0s, fraction non-zero for single precision: Infinity: exponents all 1s, fraction all 0s sign bit determines +infinity or -infinity Not a Number (NaN): exponents all 1s, A good reference is available at http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
Bit Patterns and Logical Operations OR NOT Exclusive OR They are bit-wise operations.
AND Operation OR Operation x AND y is true, if and only if both bits x and y are true. A = 1 1 0 0 1 0 1 1 B = 0 1 1 0 1 1 0 1 C=A AND B = 0 1 0 0 1 0 0 1 OR Operation x OR y is true, if either bits x or y is true. A = 1 1 0 0 1 0 1 1 B = 0 1 1 0 1 1 0 1 C=A OR B = 1 1 1 0 1 1 1 1
EOR (Exclusive OR) Operation NOT Operation A 1 becomes 0, and a 0 becomes 1. A = 1 1 0 0 1 0 1 1 C = NOT A = 0 0 1 1 0 1 0 0 EOR (Exclusive OR) Operation It is true, if and only if just one of inputs is true. A = 1 1 0 0 1 0 1 1 B = 0 1 1 0 1 1 0 1 C = A EOR B = 1 0 1 0 0 1 1 0