1

I'm trying to understand two's complement:

Does two's complement mean that this number is invalid:

1000 

Does two's complement disallow the use of the most significant bit for positive numbers. Ie. Could

1000 

Ever represent 2^3? Or would it represent -0?

I'm also confused about why you need to add 1 to a one's complement.

4
  • possible duplicate of How are negative numbers represented in 32-bit signed integer? Commented Aug 24, 2012 at 4:55
  • twos complement only works on set bit widths, like 32 bits, with the most significant bit is the complement bit Commented Aug 24, 2012 at 4:56
  • so 1000 is actually 00000000000000000000000000001000 Commented Aug 24, 2012 at 4:57
  • Work through a few examples by hand, you'll get a much better feel for what 2's complement is, and why computers represent integers that way, than you will by reading more about it. Commented Aug 24, 2012 at 10:11

4 Answers 4

1

in two's complement the MSB (most significant bit) is set to one for negatives. to multiply by -1 a two's complement number you well do the following:

 add one to the number. reverse all bits after the first one. 

for example:
the number 10010 after adding one you well get: 10011 after reversing you get: 01101.
that means that 10010 is negative 13.

the number 1000 after adding one is: 1001 after reversing: 0111.
that means that 1000 is negative 7.

now, to your last question: no. if you work with two's complement you can't use the MSB for positive numbers. but, you could define you are not using two's complement and use higher positive numbers.

Sign up to request clarification or add additional context in comments.

Comments

1

Twos-complement is based on two requirements:

  • numbers are represented by a fixed number of bits;
  • x + -x = 0.

Assuming a four bit representation, say, we have

0 + -0 = 0000 + -0000 (base 2) = 0000 => -0000 = 0000 1 + -1 = 0001 + -0001 (base 2) = 0000 => -0001 = 1111 (carry falls off the end) 

Now we have our building blocks, a drop of induction will show you the "flip the bits and add 1" algorithm is exactly what you need to convert a positive number to its twos-complement negative representation.

Comments

1

2's complement is mostly a matter of how you interpret the value, most math* doesn't care whether you view a number as signed or not. If you're working with 4 bits, 1000 is 8 as well as -8. This "odd symmetry" arises here because adding it to a number is the same as xoring it with a number (since only the high bit is set, so there is no carry into any bits). It also arises from the definition of two's complement - negation maps this number to itself.

In general, any number k represents the set of numbers { a | a = xk mod n } where n is 2 to the power of how many bits you're working with. This perhaps somewhat odd effect is a direct result of using modular arithmetic and is true whether you view number as signed or unsigned. The only difference between the signed and unsigned interpretations is which number you take to be the representative of such a set. For unsigned, the representative is the only such a that lies between 0 and n. For signed numbers, the representative is the only such a that lies between -(n/2) and (n/2)-1.

As for why you need to add one, the goal of negation is to find an x' such that x' + x = 0. If you only complemented the bits in x but didn't add one, x' + x would not have carries at any position and just sum to "all ones". "All ones" plus 1 is zero, so adding one fixes x' so that the sum will go to zero. Alternatively (well it's not really an alternative), you can take ~(x - 1), which gives the same result as ~x + 1.

*Signedness affects the result of division, right shift, and the high half of multiplication (which is rarely used and, in many programming languages, unavailable anyway).

Comments

0

It depends on how many bits you use to represent numbers.

The leftmost (largest) bit has a value of -1*(2**N-1) or in this case, -8. (N being the number of bits.) Subsequent bits are their normal values.

So

1000 

is -8

1111 

is -1 0111 is 7.

However, if you have 8 bits these become different values! 0000 1000

is positive 8. Only the leftmost bit adds a negative value to the answer.

In either case, the range of numbers is from 1000....0

for -2**(N-1) with N bits to

0111....1 Which is 2**(N-1) -1. (This is just normal base 2 since the leftmost bit is 0.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.