Download presentation
Presentation is loading. Please wait.
Published byRosamund Chandler Modified over 9 years ago
1
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 2
2
+ Outline Representing Signed Numbers Sign and Magnitude 1’s Complement 2’s Complement Signed vs. Unsigned Numbers Data Storage
3
+ Representing Signed Numbers So far, examples of unsigned numbers 3 ways for handling negative numbers Sign and Magnitude 1’s Complement 2’s Complement
4
+ Sign and Magnitude Easiest solution: Define leftmost bit to be the sign bit. 0 positive number, 1 negative number Other bits represent numerical value of number 32-bit example: 0000 0000 0000 0000 0000 0000 0000 0001 +1 10 1000 0000 0000 0000 0000 0000 0000 0001 -1 10
5
+ Convert Decimal to Sign and Magnitude: S&N requires two things: Sign: 0 positive, 1 negative Magnitude: value to be represented 16-bit Example: +74 10 0000 0000 0100 1010 16-bit Example: -74 10 1000 0000 0100 1010
6
+ Convert Decimal to Sign and Magnitude: 8-bit Example: +23 10, -23 10 +23 10 0001 0111 -23 10 1001 0111
7
+ Convert Sign and Magnitude to Decimal: 8-bit Example: 1000 0111 2, -7 10 Another Example: 1000 0000 2 0000 0000 2 Produces: -0 and +0 problems with this?
8
+ Sign and Magnitude – Not a Good Idea Arithmetic circuit more complex Special steps depending on whether signs are the same or not Also, two zeroes: 0x00000000 = +0 10 0x80000000 = -0 10 How would this affect programming languages? Sign and magnitude abandoned.
9
+ Another try: Complement the bits Example: 7 10 = 00111 2, -7 10 = 11000 2 Called 1’s Complement Note: Positive numbers have leading 0s, negative numbers have leading 1s. How many positive numbers in N bits? How many negative numbers in N bits?
10
+ Another try: Complement the bits 4-bit Example: 1’s comp representation of a 4 bit number 2 n-1 -1 positive numbers 2 n-1 -1 negative numbers PosNeg 000001111 100011110 200101101 300111100 401001011 501011010 601101001 701111000
11
+ Converting Decimal to 1’s Comp Algorithm for Converting Decimal to 1s comp: Ignore the minus sign, and convert the Decimal value to N bits, unsigned. If there is a minus sign, flip all bits of the N bit number from previous step. Otherwise, the previous step has the correct answer.
12
+ Converting Decimal to 1’s Comp Example 8-bit: -48 10 First, convert to Binary 8-bits: 0011 0000 2 Next, compliment the bits: 1100 1111 2
13
+ Converting Decimal to 1’s Comp Try 16-bit: -233 10 First, convert to Bin: 0000 0000 1110 1001 2 Next, since we’re dealing with a negative number, apply 1s comp: 1111 1111 0001 0110 2
14
+ Converting 1’s Comp to Decimal Algorithm for Converting1s comp to Decimal: If the MSB is 1, compliment all bits, otherwise move to next step. Convert the result to Base 10. This should be a non-negative value. Add a negative sign if the MSB of the 1s comp representation was 1.
15
+ Converting 1’s Comp to Decimal Example 8-bit: 1101 1110 2 MSB is 1 so compliment the bits: 0010 0001 2 Next, convert to Base 10: 33 10 Since the MSB is 1, the Base 10 value is negative: -33 10
16
+ Converting 1’s Comp to Decimal Try 16-bit: 1101 1111 0010 1100 2 MSB is 1 so compliment the bits: 0010 0000 1101 0011 2 Next, convert to Base 10: 8403 10 Since the MSB is 1, the Base 10 value is negative: -8403 10
17
+ 1’s Complement Shortcomings Arithmetic not too difficult. But still two zeroes 1’s complement eventually abandoned since another solution is better.
18
+ Still Searching for Negative Number Representation Sign and Magnitude didn’t work, 1’s Complement didn’t work..find another way. Keep representation of leading 0s pos, leading 1s neg. 000000…xxx is >= 0, 111111…xxx < 0
19
+ 2’s Complement Number Line 2 N-1 -1 Pos Nums 2 N-1 Neg Nums 1 zero Must specify width 8-bits, 16-bits, 32-bits Overflow? More on this later
20
+ 2’s Complement 2 4 bits 2’s CompDecimal 01117 01106 01015 01004 00113 00102 00011 00000 1111 1110-2 1101-3 1100-4 1011-5 1010-6 1001-7 1000-8
21
+ Converting Decimal to 2’s Comp Algorithm for Converting Decimal to 2s comp: If the number is positive, convert to N-bit unsigned Binary. Finished. Otherwise, if the number, X, is negative, convert the ABS(X) to unsigned Binary. Perform 1s comp on the unsigned Binary number. Add 1 to this number to complete 2s comp.
22
+ Converting Decimal to 2’s Comp Example 8-bit: -27 10 First, convert to Binary 8-bits: 0001 1011 2 Next, 1s comp: 1110 0100 2 Add 1 to 1s comp value: 1110 0100 2 +0000 0001 2 1110 0101 2
23
+ Converting Decimal to 2’s Comp Try 16-bit: -194 10 First, convert to Binary 16-bits: 0000 0000 1100 0010 2 Next, 1s comp: 1111 1111 0011 1101 2 Add 1 to 1s comp value: 1111 1111 0011 1101 2 +0000 0000 0000 0001 2 1111 1111 0011 1110 2
24
+ Converting from 2s Comp to Decimal Algorithm for Converting 2s Comp to Decimal: If the MSB is 1, perform 1s comp and add 1. If the MSB is 0, go to next step. Convert the result to Base 10. This value should be positive. If the MSB was 1, add the negative sign to the Base 10 number.
25
+ Converting 2’s Comp to Decimal Example 8-bit: 1010 1010 2 MSB is 1 so compliment the bits: 0101 0101 2 And add 1: 0101 0101 2 +0000 0001 2 0101 0110 2 Since the MSB is 1, the Base 10 value is negative: -86 10
26
+ Converting 2’s Comp to Decimal Try 16-bit: 1100 0010 1101 1110 2 MSB is 1 so compliment the bits: 0011 1101 0010 0001 2 And add 1: 0011 1101 0010 0001 2 +0000 0000 0000 0001 2 0011 1101 0010 0010 2 Since the MSB is 1, the Base 10 value is negative: -15,650 10
27
+ 2’s Complement 2 4 bits 2’s CompDecimalUnsigned 011177 011066 010155 010044 001133 001022 000111 000000 111115 1110-214 1101-313 1100-412 1011-511 1010-610 1001-79 1000-88
28
+ 2’s Comp Overflow Overflow is caused when N-bit arithmetic results will not fit within N- bits. Example: 8-bit 2s comp 104 + 45 = 149 149 > 2 8 The result of this addition produces an overflow. In 8-bit 2s comp, this will result in a negative number (1001 0101 2 )
29
+ 2’s Comp Overflow Rules for detecting 2s Comp overflow: If the sum of two Pos numbers yields a Neg result, the sum has overflowed. If the sum of two Neg numbers yields a Pos result, the sum has overflowed. Otherwise, the sum has not overflowed.
30
+ 2’s Comp Overflow Rules for detecting 2s Comp overflow: If the sum of two Pos numbers yields a Neg result, the sum has overflowed. If the sum of two Neg numbers yields a Pos result, the sum has overflowed. Otherwise, the sum has not overflowed.
31
+ 2’s Comp Overflow Examples -39 + 92 = 53 1 1 1 1 1 1 0 1 1 0 1 1 +0 1 0 1 1 1 0 0 0 0 1 1 0 1 0 1 Carryout without overflow. Sum is correct
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.