(root)/Notes/Notes/notes/complement.md RSS

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 1

digits.map(|digit| base - digit) + 1

note 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 0x1234

0xFFFF - 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 of 5, 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 to 2 with respect to both the 2-adic metric and the real metric; 11111110. is close to ..2 with respect to only the 2-adic metric. consequently, their sum 100000000. is close to 0 with respect to only the 2-adic metric
  • 00000001. is close to 3-3 with respect to both the 2-adic metric and the real metric; .01010101 is close to 1-3 with respect to only the real metric. consequently, their sum 00000001.01010101 is close to 4-3 with respect to only the real metric
  • 10101011. is close to 1-3 with respect to only the 2-adic metric; consequently, its triple 1000000001. is close to 1 with respect to only the 2-adic metric
  • .10101010 is close to 2-3 with respect to only the real metric; consequently, its triple 1.11111110 is close to 2 with respect to only the real metric
  • 10101011. is close to 1-3 with respect to only the 2-adic metric; .10101010 is close to 2-3 with respect to only the real metric. consequently, their sum 10101011.01010101 is close to 1 with respect to neither the 2-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 A and B, feed in A and B to get A : B as output
  • to subtract B from A, feed in A and +B and set CIN to get A..B as 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 1 at the end (or without setting the CARRY IN bit of an adder)