algorithmmathradixinternal-representation

Excess (-1) in base 4 representation


I've been trying to wrap my head around this one problem for the last couple of days, and I can't figure out a way to solve it. So, here it goes:

Given the base 4(that is 0, 1, 2, 3 as digits for a number), find the excess (-1) in base 4 representation of any negative or positive integer number. examples: -6 = (-1)22 conversely, (-1)22 in excess (-1) of base 4 = 2 * 4^0 + 2 * 4^1 + (-1) * 4^2 = 2 + 8 - 16 = 10 - 16 = -6 in base 10

27 = 2(-1)(-1) conversely, 2(-1)(-1) = (-1) * 4^0 + (-1) * 4^1 + 2 * 4^2 = -1 - 4 + 32 = 27

I did come up with a few algorithms for positive numbers, but none of them hold true for all negative numbers, so into the trash they went.

Can anyone give me some kind of clue here? Thanks!

----------------

Edit: I'm going to try to rephrase this question in such a way that it does not raise any confusions.

Consider the radix obtained by subtracting 1 from every digit, called the excess-(-1) of base 4. In this radix, any number can be represented using the digits -1, 0, 1, 2. So, the problem asks for an algorithm that gets as an input any integer number, and gives as output the representation of that given number.

Examples:

decimal -6 = -1 2 2 for the excess-(-1) of base 4.

To verify this, we take the representation -1 -1 2 and transform it to a decimal number, start from the right-most digit and use the generic base n to base 10 algorithm, like so:

number = 2 * 4^0 + 2 * 4^1 + (-1) * 4^2 = 2 + 4 - 16 = -6

2 * 4^0 + 2 * 4^1 + (-1) * 4^2 = 2 + 4 - 16 = -6


Solution

  • I don't know if "quaterit" is the correct word for the radix in this representation, but I'm going to use it anyway.

    Since you say you already have an algorithm for positive numbers, I'll try to take a negative number as an input and write something that uses what you already have. The code below doesn't quite work, but I'll explain why at the end.

    int[] BaseFourExcessForNegativeNumbers(int x) {
        int powerOfFour = 1;
        while (-powerOfFour > x) {
            powerOfFour *= 4;
        }
        int firstQuaterit = -1;
        int remainder = x + powerOfFour;
        int[] otherQuaterits;
        if (remainder >= 0) {
            otherQuaterits = BaseFourExcessForPositiveNumbers(remainder);
        } else {
            otherQuaterits = BaseFourExcessForNegativeNumbers(remainder);
        }
        int[] result = new int[otherQuaterits.Length + 1];
        result[0] = firstQuaterit;
        for (int index = 0; index < otherQuaterits.Length; ++index) {
            result[index + 1] = otherQuaterits[index];
        }
        return result;
    }
    

    The idea here is that every negative number x will start with a (-1) in this representation. If that (-1) is in the 4^n position, we want to find out how to represent x - (-1)*4^n to see how to represent the rest of the number.

    The reason the code I wrote won't work is that it doesn't take into consideration the possibility that the second quaterit is a 0. If that happens, the array my code will produce will be missing that 0. In fact, if BaseFourExcessForPositiveNumbers is written in the same way, the resulting array will be missing every 0, but will otherwise be correct. A workaround is to keep track of which place the first quaterit takes, and then make the array that size, and fill it from the back to the front.