C#
C#

help

Root Question Message

Gustavо
Gustavо2/9/2023
❔ Implementing shifts for 256 bit integer class

I'm writing a custom 256 bit int class for Godot. Internally it is stored as 4 ulong types. I've tried the following implementation but it has odd bugs such as shifts working for some numbers, but not all:
public static UInt256 operator >> (UInt256 a, int b)
    {
        ulong[] result = new ulong[4];
        int shift = b % 256;
        int fullShift = b / 256;

        for (int i = 0; i < 4; i++)
        {
            int index = (i - fullShift + 4) % 4;
            result[i] = a.values[index] >> shift;
            if (i > 0)
            {
                int shiftAmount = 64 - shift;
                result[i] |= a.values[(index - 1 + 4) % 4] << shiftAmount;
            }
        }

        return new UInt256(result[0], result[1], result[2], result[3]);
    }

    public static UInt256 operator << (UInt256 a, int b)
    {
        ulong[] result = new ulong[4];
        int shift = b % 256;
        int fullShift = b / 256;

        for (int i = 0; i < 4; i++)
        {
            int index = (i + fullShift) % 4;
            result[i] = a.values[index] << shift;
            if (i < 3)
            {
                int shiftAmount = 64 - shift;
                result[i] |= a.values[(index + 1) % 4] >> shiftAmount;
            }
        }

        Godot.GD.Print(result[0], result[1], result[2], result[3]);
        return new UInt256(result[0], result[1], result[2], result[3]);
    }
AntonC
AntonC2/9/2023
it should be a struct not a class
AntonC
AntonC2/9/2023
also use stackalloc for those temporary arrays
AntonC
AntonC2/9/2023
well your code is just wrong
AntonC
AntonC2/9/2023
it doesn't handle shift of more than 64 bits
AntonC
AntonC2/9/2023
also take a byte instead of an int, or do a range check
AntonC
AntonC2/9/2023
shifting by an amount larger than the size of a number is UB I think
AntonC
AntonC2/9/2023
ah nevermind it does handle larger values
AntonC
AntonC2/9/2023
well, must be some logical error
AntonC
AntonC2/9/2023
ah I see it
AntonC
AntonC2/9/2023
divide by 64 not by 256
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy