COSC2410: LAB 2 BINARY ARITHMETIC SIGNED NUMBERS FLOATING POINT REPRESENTATION BOOLEAN ALGEBRA 1
Review Convert from base 10 to 2, 8, Base 2: Base 8: 51 Base 16: 29 2
BINARY ARITHMETIC ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION 3
Binary Arithmetic: Addition and Subtraction Addition = = = = 0 (carry: 1) Subtraction 0 – 0 = 0 0 – 1 = 1 (borrow: 1) 1 – 0 = 1 1 – 1 = 0 4
Binary Addition: Example Note: Incase of adding 3 1’s. Instead of the sum being 0 with a carry of 1, the sum is 1 with a carry of 1. 5
Binary Subtraction: Examples Note: After borrowing from the next digit, 0 becomes 1 0 and not becomes 1 when borrowed from. 6
Binary Addition and Binary Subtraction Practice 1. Add and Subtract 1011 from Add and Subtract 010 from
Binary Arithmetic Multiplication Multiplication 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1 Note: When multiplying, add two of the partial products at a time to prevent mistakes that my occur from adding many numbers 8
Binary Multiplication Practice 1. Multiply 1001 and Multiply 1101 and Multiply 0001 and Multiply 1010 and
SIGNED NUMBERS 3 WAYS TO REPRESENT 10
Method 1: Sign And Magnitude Obvious solution: sign and magnitude define leftmost bit (most significant bit) to be sign! 0 => +(positive), 1 => - (negative) Rest of bits can be numerical value of number MIPS uses 32-bit integers. +1then would be: And - 1 in sign and magnitude would be:
Method 2: 1’s Complement Complement the bits Example: 7 10 = = What is ?
Method 3: 2’s Complement Method 1: Subtract from 2 n. For Example 3: 0011, n = 4 -3: (2 n ) – 0011 = 1101 Method 2: Complement and add 1 3: : = 1101 The leftmost bit of a signed binary numeral indicates the sign 0 (+), all the bits are the numerical value of number 1 (-), all the bits are inverted then 1 is added to the result ► Remember for positive numbers, just make sure a 0 is the MSB. You do not need to convert it using the formula above! 13
Signed Number Representation Table 14
Signed Number Representation Practice 1. Represent each negative number as an 8 bit binary number using a) sign and magnitude, b) 1’s complement, and c) 2’s complement
Addition/Subtraction using 2’s Complement Subtraction = Addition with opposite sign. 4 – 6 = 4 + (-6) = -2 4: :1010(2’s complement) Sign bit =-2
Overflow Error: Sum Is Too Large 4 bit binary with 2’s complement representation has a number range between -8 and 7 What happens if 6 + 4? (-7) + (-2)? 6:0110-7:1001 4:0100-2: Sign bit = 1 (negative) Sign bit = 0 (positive)
Overflow Error: Cont’d… Only 2 cases where overflow can occur: Sum of 2 positive numbers yields a negative result Sum of 2 negative numbers yields a positive result Note: We always ignore the carry to ensure number of bits does not change. 18
2’s Complement Addition/Subtraction Practice Convert the following decimal numbers into 6 bit binary and then solve using 2’s complement. Indicate if Overflow Occurs
IEEE FLOATING POINT REPRESENTATION 20
IEEEFloating Point Representation Most standard floating point representation use: 1 bit for the sign (positive or negative) 8 bits for the range (exponent field) 23 bits for the precision (fraction field) 21 Note: Biased Notation. The exponent is obtained by adding 127 to the value. That is: 5 -> 132, 1-> 128, 0 -> 127, >0
Floating Point Exceptions Overflow & Underflow Overflow occurs when the exponent is too large to fit in the exponent field. Underflow occurs when the exponent is too small to fit in the exponent field. 22 Negative Overflow Negative numbersPositive Numbers Negative UnderflowPositive UnderflowPositive Overflow 0
Floating Point Example How is the decimal number represented in floating point? Show Steps of your work. 23 Thus the exponent is given by:
Floating Point Example Solution
Floating Point Example 2 Convert the following binary floating point number into decimal FPexponent = = 125
Floating Point Representation Practice How is the decimal number represented in floating point? Show all steps of your work. How is the number (17.4) 16 represented in binary floating point? Show all steps of your work. Represent ( ) 2 as floating point. Show all steps of your work. 26
Floating Point Practice Cont’d… 27 Convert the following binary floating point number into an un-normalized binary number Convert the following binary floating point number into decimal