Why didn't the complement's formula work?

2019-09-04 03:28发布

问题:

I have just learnt that to get the formula to find the 1st Complement is

    -x = 2^n - x - 1

I have managed to apply it on a binary case:

     -00001100 (base 2) = 2^8 - 12 - 1 
                        = 243
                        = 11110011 (1s)

However, when I try to apply the same formula to a base 5 number,

     -1042 (base 4) = 5^4 - 1042 - 1
                    = 625 - 1042 - 1
                    = - 400 (which is not the answer)

Can some one help me out here? Thanks

回答1:

you cannot calculate any formula with numbers in 2 different bases, you have to use their decimal representation (or an other representation you can handle)

I'll give it a try in dec:

1042 (base 5) = 1* 5^3 + 4* 5^1 + 2 = 125 + 20 + 2 = 147 dec
5^4 - 147 - 1 = 477 dec
477 = 3* 5^3 + 4* 5^2 + 2 = 3402 (base 5)

in base 5:

5^4 - 1042 - 1 = 10000 - 1043 = 3402