How do I represent negative numbers in non-10 bases, eg say in base 20. I know how to do this in binary using two's complement, but what would the equivalent of two's complement be in other bases?
For example, in base 20, the denary number 100
is represented as 50
. How would I make this 50
signed? Would I need to convert it to binary, two's complement it, the convert it back to base-20? It seems a little long-winded.
In that case, negative base-20 50
(which is 100 in base-10) would be 7g
, and positive would be just 50
. But is this the standard way to represent negative numbers in other bases?
The generalisation of two's complement is radix complement.
The radix complement of an 𝑛 digit number y in radix 𝑏 is, by definition, 𝑏𝑛−𝑦. The radix complement is most easily obtained by adding 1 to the diminished radix complement, which is (𝑏𝑛−1)−𝑦
We must however agree what 𝑛 is. For instance, in binary we may say we have 32 bits, so 𝑛 = 32 in that case.
So for the example of base-20 we should make some definitions:
We can also define what the "(diminished) complementary digit" is for each digit. It is the digit that when added to the first will always yield the greatest digit (𝑗20 in this case). For example 220 + ℎ20 = 𝑗20, so ℎ20 is the complement of 220, and vice versa.
For your example number 5020, we proceed as follows:
Replace every digit of the 𝑛-digit representation with its complement:
So the diminished complement of 000000005020 thus becomes 𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑒𝑗20
To get the negation of 5020 we just need to add 1 to that:
−5020 = 𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑗𝑓020
It is important to not reduce this representation to use fewer digits -- we have to stick with the 𝑛-digit representation, otherwise it is not clear when a number is positive or negative.