Understanding the Effects of 2's Complement on Arithmetic Operations

In summary, when using 2's complement, the result of arithmetic operations can differ by two due to the algorithm for dealing with negative numbers. To convert a negative number to 2's complement, the original value must be complemented and 1 must be added, and to find the decimal value of a 2's complement number, the original value must be complemented, 1 must be added, and a negative sign must be applied to the result.
  • #1
geft
148
0
Why is it that when using 2's complement, the result of arithmetic operations differ by two?

11011001 (-39) +
11100111 (-25) =
11000000 (which is -62 in 2's complement, even though it's supposed to be -64)

00110011 (51) +
11101110 (-16) =
00100001 (33, but it's supposed to be 35)
 
Physics news on Phys.org
  • #2
Some errors:

-64 is 11000000 in 2's complement.

-16 is 11110000 in 2's complement.
 
  • #3
64 is 11000000 in 2's complement.

Am I doing it wrong?
11000000
=> 00111110
= 2^1 + 2^2 + 2^3 + 2^4 + 2^5 = 62
=> -62

16 is 11110000 in 2's complement.

11110000
=> 00001110
= 2^1 + 2^2 + 2^3 = 14
=> -14
 
  • #4
geft said:
Am I doing it wrong?
11000000
=> 00111110
Looks like you're subtracting one after inverting the bits? I believe you always add one, so it should be
00111111 + 1 = 01000000

and
11110000
=> 00001110
00001111 + 1 = 00010000
 
  • #5
Agree with eumyang.

The algorithm for dealing with negative numbers (positives are easy--do nothing) is this :

To convert, say -64 into 8-bit 2's complement:
1) Take the 8-bit binary of the positive part (64): 0100 0000
2) Since the original value is negative, complement the result from 1) and add 1: 1011 1111 + 1 = 1100 0000


To find the decimal given the 8-bit 2's complement value, say for 1100 0000:
1) Note that the MSB is a 1 and therefore your result will be a negative number.
2) Complement the original value: 1100 0000 => 0011 1111
3) Add 1: 0011 1111 + 1 = 0100 0000 (which is binary 64)
4) Apply the negative sign from part 1) to the result from part 3) "-" : 64 = -64
 
  • #6
I see where I was wrong... I thought I didn't need to change the last bit in any way, so when inverting I always leave the last bit alone. Thanks!
 

Related to Understanding the Effects of 2's Complement on Arithmetic Operations

1. What is the purpose of using signed binary in arithmetic?

Signed binary is used to represent both positive and negative numbers in a computer system. It allows for a wider range of numbers to be represented using a fixed number of bits, making it more efficient for calculations.

2. How do you perform addition with signed binary numbers?

To add signed binary numbers, you can use the same rules as regular binary addition, but with the addition of a sign bit. If the sign bit is 0, the number is positive, and if it is 1, the number is negative. The carry from the sign bit is then added back into the final result.

3. Can you explain how subtraction works with signed binary numbers?

Subtraction with signed binary numbers follows the same rules as addition, but with the use of two's complement. To subtract a negative number, you first take the two's complement of the number, then add it to the first number using regular binary addition. The carry bit can then be discarded.

4. How do you handle overflow in signed binary arithmetic?

Overflow occurs when the result of an arithmetic operation is too large to be represented in the given number of bits. In signed binary, overflow can be detected by checking if the carry out of the sign bit is different from the carry into the sign bit. If they are different, overflow has occurred and the result is not accurate.

5. What is the difference between signed and unsigned binary arithmetic?

Signed binary arithmetic allows for the representation of both positive and negative numbers, while unsigned binary arithmetic can only represent positive numbers. Signed binary also uses a sign bit to indicate the sign of a number, while unsigned binary does not. Additionally, the rules for addition and subtraction are slightly different between the two types of arithmetic.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
16K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
4K
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Computing and Technology
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Back
Top