I have a program that is interfacing with an external library that, among other things, has an unsigned 12-bit value packed in a larger struct.
This used to be 8 bit
You should create a struct that overrides the implicit conversion operator:
struct PackedValue {
private PackedValue(ushort val) {
if(val >= (1<<12)) throw new ArgumentOutOfRangeException("val");
this._value = val;
}
private ushort _value;
public static explicit operator PackedValue(ushort value) {
return new PackedValue(value);
}
public static implicit operator ushort(PackedValue me) {
return me._value;
}
}