Question

Do you know an algorithm or a way to find out the complement of a number in base N.?

      For example number 001112 which is in base 3 .

Thank you

Was it helpful?

Solution

For a number with k digits, subtract it from (Nk)N (perform the subtraction in base N, of course).

OTHER TIPS

For the definition of the complement see http://www.enggpedia.com/computer-engineering-encyclopedia/dictionary/dgital-logic-design/1603-complements-types-of-complements-radix-a-diminished-radix-complement.
In your example the complement is 3^6 - 001112(base 3) = (333333+1-001112)(base 3) = (332221+1)(base 3) = 332222.

EDIT:

The comments below are of course right. Sorry for posting such a wrong example. Let me try to reformulate what I wanted to say (and please correct me if I am wrong again):

Your example, expressed in base 3, is:

 1000000
- 001112
--------
= 221111  

The question is if a final 1 should be added.
If one has only unsigned numbers, there is no need to add a 1, but the complement does not have an arithmetic significance.
If one has signed numbers, the complement can be interpreted as a kind of the negative of the original number, but not as the negative itself. An example in base 2:
If n = 01101, the 1's complement of n is 10010. If we add both, the result is of course 11111 which is not zero. Thus, the 1's complement of n is NOT -n. But if we add a final 1, we get 00000, i.e. zero. Therefore the result of building the 1's complement and adding a 1 is called the 2's complement, and this CAN be interpreted as -n.
I assume the same holds true for any base.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top