public static long UnionStruct() { long sum = 0; for (int i = 0; i < _iterations; i++) { byte tf = (byte)(i % 255 + 1); s_true = Unsafe.As <byte, bool>(ref tf); for (int n = 0; n <= N; n++) { sum += BoolToByte.AsByte(s_true); sum++; sum -= BoolToByte.AsByte(s_false); sum--; sum += BoolToByte.If(s_true, 4); sum -= BoolToByte.If(s_false, 3); sum += BoolToByte.If(s_true, 3, 2); sum -= 7; } } return(sum); }
public static uint AsByte(bool condition) { // Normalize bool's underlying value to 0|1 // https://github.com/dotnet/roslyn/issues/24652 int val = new BoolToByte { Bool = condition }.Byte; // CLR permits 0..255 val = -val; // Negation will set sign-bit iff non-zero val >>= 31; // Send sign-bit to lsb (all other bits will be zero) return((byte)val); }