Presentation is loading. Please wait.

Presentation is loading. Please wait.

James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.

Similar presentations


Presentation on theme: "James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How."— Presentation transcript:

1 James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How are subtractions performed by the computer?

2 James Tam Subtraction In the real world A - B In the computer A - B

3 James Tam Subtraction In the real world A - B In the computer A - B Not done this way! A + (-B)

4 James Tam Binary Subtraction Requires the complementing of a binary number -i.e., A – B becomes A + (-B) The complementing can be performed by representing the negative number as a One’s or Two’s complement value.

5 James Tam Complementing Binary Using The Ones Complement Representation For positive values there is no difference (no change is needed) -e.g., positive seven 0111 (regular binary) For negative values complement the number by negating the binary values: reversing (flipping) the bits (i.e., a 0 becomes 1 and 1 becomes 0). -e.g., minus six -0110 (regular binary) 0111 (1’s complement equivalent) 1001 (1’s complement equivalent)

6 James Tam Complementing Binary Using The Twos Complement Representation For positive values there is no difference (no change is needed) -e.g., positive seven 0111 (regular binary) For negative values complement the number by negating the number: reversing (flipping) the bits (i.e., a 0 becomes 1 and 1 becomes 0) and adding one to the result. -e.g., minus six -0110 (regular binary) 0111 (2’s complement equivalent) 1010 (2’s complement equivalent)

7 James Tam Representing Negative Numbers Real world -Negative numbers – same as the case of positive numbers but precede the number with a negative sign “-” e.g., -123456. Computer world -Negative numbers – employ signed representations.

8 James Tam Magnitude Of Non-Computer Representations All of the digits are used to represent the magnitude of the number -e.g., 175 10, 1001 2 An explicit minus sign is needed to distinguish positive and negative numbers -e.g., 124 10 vs. -124 10 or 100 2 vs. -100 2

9 James Tam Signed Binary: Magnitude One bit (most significant bit/MSB or the signed bit) is used to indicate the sign of the number. This bit cannot be used to represent the magnitude of the number If the MSB equals 0, then the number is positive -e.g. 0 bbb is a positive number (bbb stands for a binary number) If the MSB equals 1, then the number is negative -e.g. 1 bbb is a negative number (bbb stands for a binary number) Types of signed representations -Ones complement -Twos complement

10 James Tam Signed Binary: Magnitude One bit (most significant bit/MSB or the signed bit) is used to indicate the sign of the number. This bit cannot be used to represent the magnitude of the number If the MSB equals 0, then the number is positive -e.g. 0 bbb is a positive number (bbb stands for a binary number) If the MSB equals 1, then the number is negative -e.g. 1 bbb is a negative number (bbb stands for a binary number) Types of signed representations -Ones complement -Twos complement Positive Negative

11 James Tam Interpreting The Pattern Of Bits Bit patternRegular binaryOnes complementTwos complement 0000000 0001111 0010222 0011333 0100444 0101555 0110666 0111777 10008-7-8 10019-6-7 101010-5-6 101111-4-5 110012-3-4 110113-2-3 111014-2 111115-0

12 James Tam Overflow: Regular Binary Occurs when you don't have enough bits to represent a value (wraps –around to zero) Binary (1 bit) Value 00 11 Binary (2 bits) Value 000 011 102 113 Binary (3 bits) Value 0000 0011 0102 0113 1004 1015 1106 1117 0: 00 0 : 000 0 :

13 James Tam Overflow: Signed In all cases it occurs do to a “shortage of bits” Subtraction – subtracting two negative numbers results in a positive number. e.g. - 7 - 1 + 7 Addition – adding two positive numbers results in a negative number. e.g. 7 + 1 - 8

14 James Tam Summary Diagram Of The 3 Binary Representations Regular binary Ones complement Twos complement

15 James Tam Summary Diagram Of The 3 Binary Representations Regular binary Ones complement Twos complement Overflow

16 James Tam Binary Subtraction Via Complement And Add: A High-Level View I only speak binary What is x – y (in decimal)?

17 James Tam Binary Subtraction Via Complement And Add: A High-Level View I only do subtractions via complements

18 James Tam Binary Subtraction Via Complement And Add: A High-Level View 1) Convert the decimal values to regular binary 4) Convert the complements to regular binary 2) Convert the regular binary values to complements 5) Convert the regular binary values to decimal 3) Perform the subtraction via complement and add This section

19 James Tam Crossing The Boundary Between Regular And Signed Binary Regular binary One's complement Two's complement Each time that this boundary is crossed (steps 2 & 4 from the previous slide) apply the rule: 1) Positive numbers pass unchanged 2) Negative numbers must be converted (complemented) a.One’s complement: negate the negative number b.Two’s complement: negate and add one to the result

20 James Tam Binary Subtraction Through Ones Complements 1)Convert from regular binary to a 1's complement representation (check if it is preceded by a minus sign). a.If the number is not preceded by a minus sign, it’s positive (leave it alone). b.If the number is preceded by a minus sign, the number is negative (complement it by flipping the bits) and remove the minus sign. 2)Add the two binary numbers. 3)Check if there is overflow (a bit is carried out) and if so add it back. 4)Convert the 1’s complement value back to regular binary (check the value of the MSB). a.If the MSB = 0, the number is positive (leave it alone) b.If the MSB = 1, the number is negative (complement it by flipping the bits) and precede the number with a minus sign

21 James Tam Binary Subtraction Through 1’s Complements e.g. 01000 2 - 00010 2 1 Step 3: Check for overflow 00110 2 Step 4: Check MSB Step 1: Complement 01000 2 11101 2 Step 2: Add no.’s 01000 2 11101 2 00101 2 Step 4: Leave it alone ______ +1 2 Step 3: Add it back in

22 James Tam Binary Subtraction Through Two’s Complements 1)Convert from regular binary to a 2's complement representation (check if it’s preceded by a minus sign). a.If the number is not preceded by a minus sign, it’s positive (leave it alone). b.If the number is preceded by a minus sign, the number is negative (complement it and discard the minus sign). i.Flip the bits. ii.Add one to the result. 2)Add the two binary numbers. 3)Check if there is overflow (a bit is carried out) and if so discard it. 4)Convert the 2’s complement value back to regular binary (check the value of the MSB). a.If the MSB = 0, the number is positive (leave it alone). b.If the MSB = 1, the number is negative (complement it and precede the number with a negative sign). i.Flip the bits. ii.Add one to the result.

23 James Tam Binary Subtraction Through 2’s Complements e.g. 01000 2 - 00010 2 Step 2: Add no’s 01000 2 11110 2 00110 2 1 Step 3: Check for overflow Step 1A: flip bits 01000 2 11101 2 Step 1B: add 1 01000 2 11110 2

24 James Tam Binary Subtraction Through 2’s Complements e.g. 01000 2 - 00010 2 Step 1A: flip bits 01000 2 11101 2 Step 1B: add 1 01000 2 11110 2 Step 3: Discard it Step 2: Add no’s 01000 2 11110 2 00110 2

25 James Tam Binary Subtraction Through 2’s Complements e.g. 01000 2 - 00010 2 Step 1A: flip bits 01000 2 11101 2 Step 1B: add 1 01000 2 11110 2 Step 2: Add no’s 01000 2 11110 2 00110 2 Step 4: Check MSB Step 4: Leave it alone

26 James Tam Representing Real Numbers Via Floating Point Numbers are represented through a sign bit, a mantissa and an exponent Examples with 5 digits used to represent the mantissa: -e.g. One: 123.45 is represented as 12345 * 10 -2 -e.g. Two: 0.12 is represented as 12000 * 10 -5 -e.g. Three: 123456 is represented as 12345 * 10 1 Sign Mantissa Exponent Floating point numbers may result in a loss of accuracy!

27 James Tam You Should Now Know How negative numbers are represented using 1’s and 2’s complement representations. How to convert regular binary to values into their 1’s or 2’s complement equivalent. What is signed overflow and why does it occur. How to perform binary subtractions via the negate and add technique. How are real numbers represented through floating point representations


Download ppt "James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How."

Similar presentations


Ads by Google