Gustavo
Gustavo
CC#
Created by Gustavo on 2/9/2023 in #help
❔ 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]);
}
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]);
}
12 replies