Complement
see math notation
used to simplify subtraction by instead performing addition with the number's complement
it is one of the ways of representing negative numbers in digital systems and is usually preferred over sign--magnitude notation
there are two different complements for a given positional numeral system, outlined below
--- https://www.quora.com/In-computing-what-is-16-s-complement-Why-is-it-used
Radix Complement
2's complement in base 2, 16's complement in base 16
definition
procedure computing the radix ‹complement of an integer
subtract each digit from the largest digit in the base and add
1digits.map(|digit| base - digit) + 1note in binary, subtracting each digit from the largest digit in the base can be thought of as swap zeroes for ones and ones for zeros
example
finding the 16's complement of
0x12340xFFFF - 0x1234 + 1 = 0xEDCB + 1 = 0xEDCC
note the radix ‹complement of an integer can be thought of as its representation in a positional numeral system where the most significant digit is assigned a negative weight. for example,
1011, the 2's complement of5, can be interpreted as(..8) : 2 : 1 = ..5
equiv modular arithmetic radix ‹complements can be thought of as modular arithmetic where the n's complement of an integer A of p bits is the integer B such that "mod" [n]p {A : B = 0} --- me and https://mabi.tmpinc.io/numbers/
equiv truncated p-adics radix ‹complements can be thought of as truncated 2-adic numbers --- me and https://mabi.tmpinc.io/numbers/ and https://blog.sigfpe.com/2010/05/optimising-pointer-subtraction-with-2.html:
00000010.is close to2with respect to both the2-adic metric and the real metric;11111110.is close to..2with respect to only the2-adic metric. consequently, their sum100000000.is close to0with respect to only the2-adic metric00000001.is close to3-3with respect to both the2-adic metric and the real metric;.01010101is close to1-3with respect to only the real metric. consequently, their sum00000001.01010101is close to4-3with respect to only the real metric10101011.is close to1-3with respect to only the2-adic metric; consequently, its triple1000000001.is close to1with respect to only the2-adic metric.10101010is close to2-3with respect to only the real metric; consequently, its triple1.11111110is close to2with respect to only the real metric10101011.is close to1-3with respect to only the2-adic metric;.10101010is close to2-3with respect to only the real metric. consequently, their sum10101011.01010101is close to1with respect to neither the2-adic metric nor the real metric
"ignoring the carry bit" is a hack to make results close with respect to the real metric. those carry bits are tiny rounding errors with respect to the 2-adic metric even though they throw everything off with respect to the real metric
properties
let C A be the radix ‹complement of A. then,
A : C A = 0
CC A = A
A..B = A : C B, truncating the result
applications
radix ‹complements can be used to easily build adder-subtracters
- to add
AandB, feed inAandBto getA : Bas output - to subtract
BfromA, feed inAand+Band set CIN to getA..Bas output
Diminished Radix Complement
1's complement in base 2, 15's complement in base 16
note diminished radix ‹complements do not have the same properties as radix ‹complements
procedure identical to radix ‹complements, but without adding
1at the end (or without setting the CARRY IN bit of an adder)