Frage

I've been reading over a couple of questions/answers here:

twos-complement-in-python

is-twos-complement-notation-of-a-positive-number-the-same-number

Someone gave some sample code to create a two's complement of a number:

def twos_comp(val, bits):
    """compute the 2's compliment of int value val"""
    if( (val&(1<<(bits-1))) != 0 ):
        val = val - (1<<bits)
    return val

In addition someone defined two's complement as thus:

Two's complement notation uses the n-bit two's complement to flip the sign. For 8-bit numbers, the number is subtracted from 2^8 to produce its negative.

These declarations went unchallenged. However, that chides with my understand of a two's complement is. I thought it's calculated by inverting the binary number and adding 1. (With the understanding that the number representation has a limited number of bits.)

Additionally, the two's complement is supposed to have the property that it is the additive inverse of the original number. But, the output from twos_comp doesn't appear to have that. In my hand calculations (and some test code I wrote) with my definition , I see that when a number and the twos complement of it are added together, a 1 is overflowed and the rest of the bits are zero, thus it has the additive inverse property.

Are there multiple definitions for twos complement, am I confused, or is that definition and function from the other posts just plain wrong?

War es hilfreich?

Lösung

Twos complement is in fact calculated by inverting the binary number and adding 1, for negative numbers. Such that abs(-1)=1=01 -> bitwise_inv(abs(-1))+abs(-1)=FE+1=FF. This is equivalent to the definition provided of subtracting the number from 2^8 (this should not be hard to see).

The sample code you provided, does not calculate twos complement in any useful way. I don't at all understand whats its trying to do, appears to quite be quite different from "subtracting the number from 2^8" as subtracts 2*8 from the number, while also failing to remember that when we refer to the value of a twos complement number what we mean is its unsigned value.

Here is a more correct implementation, using the same template. Notice that this precisely does "subtract the number from 2^8."

def twos_c(val,bits):
  if ((val&(1<<(bits-1)))!=0):
    val=(1<<bits)-abs(val)
  return val
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top