I've been reading over a couple of questions/answers here:
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?