Lee Buhrmann
Lee Buhrmann

Reputation: 11

Two's Complement on representing negative numbers

I am currently trying to gain a more intuitive understanding of two's complement and its uses; however, I cannot seem to perform subtraction using two's complement correctly. I understand that when a negative number is stored in a signed int variable the procedure is to perform two's complement on the number having the MSB be 1 to represent the negative sign.

So in a 4 bit system 1010 represents -6.

Now I am following this guide on how to subtract two binary #s

For example I have the number 0101 (5 in decimal) and 1010 (-6 in decimal). If I wanted to do the equation 5 - (-6) it would look like 0101 - 1010 in binary. Next I would take the 1010 and perform twos' complement on it to get 0110. Now I take 0101 + 0110 and get 1011. I don't have a carry so I perform two's complement on the result giving me 0101, but this says the answer is -5 when it should be 11.

Upvotes: 1

Views: 600

Answers (3)

John Bollinger
John Bollinger

Reputation: 180048

For example I have the number 0101 (5 in decimal) and 1010 (-6 in decimal). If I wanted to do the equation 5 - (-6) it would look like 0101 - 1010 in binary.

Ok.

Next I would take the 1010 and perform twos' complement on it to get 0110.

Ok. And 0110 binary is 6 decimal is -(-6) decimal.

Now I take 0101 + 0110 and get 1011.

Ok.

I don't have a carry so I perform two's complement on the result giving me 0101,

I take you to mean that as part of the process of interpreting the result, not computing it. The result itself is 1011 (4-bit, two's complement binary).

but this says the answer is -5 when it should be 11.

The answer in the operational system you have chosen, with 4-bit two's complement, is -5, exactly as you have computed. With three data bits and one sign bit, the maximum result your data type can represent is 7 (0111 binary). This underscores the importance of choosing data types appropriate for the computations you want to perform.

Upvotes: 1

outis
outis

Reputation: 77400

Note that 0b1011 is 11 in binary, if interpreted as unsigned or with more than 3 data bits, so both 11 and -5 are the result (which you use depends on how its interpreted).

In terms of modular arithmetic, 4 bits is a base of 24, and:

11 ≡ -5 (mod 16)

Which answer you use depends on whether the nibble is interpreted as signed or unsigned.

Upvotes: 0

ikegami
ikegami

Reputation: 385496

4 bits can represent 16 different values. With two's complement, they are -8 to 7.

1000  -8
1001  -7
1010  -6
1011  -5
1100  -4
1101  -3
1110  -2
1111  -1
0000   0
0001   1
0010   2
0011   3
0100   4
0101   5
0110   6
0111   7

You can't possibly get an answer of 11. It's too large to fit in 4 bits as a two's complement number. As such, the calculation you are attempting results in an overflow. (See below for how to detect overflows.)

This means 4 bits is not enough to compute 5 - -6. You need at least 5 bits.

  0(...0)0101    5
- 1(...1)1010   -6
------------------
  0(...0)0101    5
+ 0(...0)0110    6
------------------
  0(...0)1011   11

Detecting Overflow in Two's Complement

With two's complement numbers, an addition overflows when the carry into the sign bit is different than the carry out of it.

  0101    5
+ 0110    6
-----------
  1011   -5   Carry in: 1  Carry out: 0  OVERFLOW!
  1011   -5
+ 1010   -6
-----------
  0101    5   Carry in: 0  Carry out: 1  OVERFLOW!
  0001    1
+ 0010    2
-----------
  0011    3   Carry in: 0  Carry out: 0  No overflow
  1111   -1
+ 1110   -2
-----------
  1101   -3   Carry in: 1  Carry out: 1  No overflow

Upvotes: 3

Related Questions