C
C#2y ago
DaVinki

✅ Code highlighting issue in Rider with Int128 and UInt128 types

Putting the new 128-bit integer classes to use which implement ISignedNumber and IUnsignedNumber. Both of those interfaces implement INumberBase and that implements IAdditionOperators. Here is the code of both UInt128 and Int128 with their operator + implementations:
public static UInt128 operator +(UInt128 left, UInt128 right)
{
// For unsigned addition, we can detect overflow by checking `(x + y) < x`
// This gives us the carry to add to upper to compute the correct result

ulong lower = left._lower + right._lower;
ulong carry = (lower < left._lower) ? 1UL : 0UL;

ulong upper = left._upper + right._upper + carry;
return new UInt128(upper, lower);
}
public static UInt128 operator +(UInt128 left, UInt128 right)
{
// For unsigned addition, we can detect overflow by checking `(x + y) < x`
// This gives us the carry to add to upper to compute the correct result

ulong lower = left._lower + right._lower;
ulong carry = (lower < left._lower) ? 1UL : 0UL;

ulong upper = left._upper + right._upper + carry;
return new UInt128(upper, lower);
}
public static Int128 operator +(Int128 left, Int128 right)
{
// For unsigned addition, we can detect overflow by checking `(x + y) < x`
// This gives us the carry to add to upper to compute the correct result

ulong lower = left._lower + right._lower;
ulong carry = (lower < left._lower) ? 1UL : 0UL;

ulong upper = left._upper + right._upper + carry;
return new Int128(upper, lower);
}
public static Int128 operator +(Int128 left, Int128 right)
{
// For unsigned addition, we can detect overflow by checking `(x + y) < x`
// This gives us the carry to add to upper to compute the correct result

ulong lower = left._lower + right._lower;
ulong carry = (lower < left._lower) ? 1UL : 0UL;

ulong upper = left._upper + right._upper + carry;
return new Int128(upper, lower);
}
How come Rider keeps telling me these types are incapable of any sort of operation? The code runs fine but is there a way to fix this inconvenience? Or at the least, disable the check here? I have already changed all projects to target .NET 7.0 and the solution's .NET SDK is 7.0. I also invalidated the caches
5 Replies
ChucklesTheBeard
known bug, is expected to be fixed in the next version Rider is a little slow to support new .NET versions also it's apparently a regression exclusive to 2022.3.1 so reverting to 2022.3 might fix it if you want to do that. https://youtrack.jetbrains.com/issue/RIDER-87010/Cannot-apply-operator-to-operands-of-type-System.UInt128-and-int
mtreit
mtreit2y ago
Just curious, what's your use case for 128 bit integers?
DaVinki
DaVinki2y ago
Many of the numbers in my program are larger than longs but no larger than long longs so I'm just avoiding the use of BigInteger But just playing around with new toys that came in .NET 7 really
mtreit
mtreit2y ago
Those...are some big numbers 🙂
DaVinki
DaVinki2y ago
For real 😂