Custom unlimited whole limits and roll
I currently need a special datatype in C #. I need the datatype to be an integer that can hold a value between 0 and 151. I already know that I can clamp the minimum and maximum spectra, but I want it to be an flip function, not a limit limiter like this, how an unsigned integer goes around to 0 when it reaches the limit. One thing I can't figure out is how to deal with the overflow. I mean something like this: suppose the value is 150 and i + = 5. The value will go back to zero and then add the remainder, which is 4. How do I do this without the overly expensive computation?
How do you implement this?
source to share
I would wrap the 151 ( % 151
) module inside a struct and declare something like:
struct UIntCustom {
public uint Value { get; private set; }
public UIntCustom(uint value) : this() {
Value = value % 151;
}
public static UIntCustom operator +(UIntCustom left, UIntCustom right) {
return new UIntCustom(left.Value + right.Value);
}
public static UIntCustom operator -(UIntCustom left, UIntCustom right) {
return new UIntCustom(left.Value - right.Value);
}
// and so on
public static explicit operator UIntCustom (uint c) {
return new UIntCustom(c);
}
}
Run example:
UIntCustom c = new UIntCustom(4);
Console.WriteLine(c.Value);
c -= (UIntCustom) 9;
Console.WriteLine(c.Value);
c += (UIntCustom) 150;
Console.WriteLine(c.Value);
Outputs:
4
150
149
source to share