1

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?

Krokodil
  • 1,165
  • 5
  • 19
  • 1
    Add a minus sign. – גלעד ברקן Oct 01 '21 at 09:53
  • 1
    @גלעדברקן This is like *sign and magnitude* in binary. Here is [why sign and magnitude is a bad method](https://stackoverflow.com/questions/1125304/why-prefer-twos-complement-over-sign-and-magnitude-for-signed-numbers). – Krokodil Oct 01 '21 at 10:02
  • 1
    I was joking, but now learned something new from trincot's answer! – גלעד ברקן Oct 01 '21 at 10:09
  • That said, I don't see how radix complement could help if you're implementing this on a computer. Most computers only perform operations on binary numbers internally and the program one would likely write would need to convert the other base to a representation the computer can operate on, meaning the minus sign could likely work just as well. – גלעד ברקן Oct 01 '21 at 10:31

1 Answers1

2

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:

  • The digits are "0123456789ℎ".
  • is 10 (an arbitrary choice, but it has to be made)

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you very much. So if I understand correctly, -50₂₀ will proceed like so: positive: `000050`, complement: `jjjjej`, add one: `jjjjf0`, therefore -50₂₀ = `jf0₂₀` when normalized. Please correct me if I'm wrong. – Krokodil Oct 01 '21 at 10:35
  • Yes, you are right. Somewhere in my answer I switched the example from 50₂₀ to 20₂₀ and then continued with that. It's now updated to work out the example of 50₂₀. However, there is no normalisation in the sense that you can just remove digits. You need to stick to an agreed number of digits. In binary-8 the number 0b11111111 is -1, but we cannot reduce that to just 0b1, because then it is just 1. The first digit (8th from the right) indicates a sign. – trincot Oct 01 '21 at 10:40
  • Ah yes, that makes sense, thank you. – Krokodil Oct 01 '21 at 11:07
  • If, however, I am representing this number using floating-point notation, then `jjjjjjf0` will become `jf000000 0002` where the first part is the 8-bit mantissa and the second is the 4-bit exponent? – Krokodil Oct 02 '21 at 13:02
  • That depends on the exact encoding rules for floating point. The most common binary floating point representations do not encode the mantissa in two's component, but in absolute value, and have a separate sign bit. – trincot Oct 02 '21 at 20:30