public static int RotateRightThroughCarry( int value, int bitCount, ref bool carry) { var bitMaskUpToEnd = BitMath.CreateBitmaskUpTo(bitCount); var bitMaskAtMsb = BitMath.CreateBitmaskAt(bitCount - 1); value &= bitMaskUpToEnd; var remaining = value >> 1; var carried = carry ? bitMaskAtMsb : 0; carry = (value & 1) == 1; return((remaining | carried) & bitMaskUpToEnd); }
public static int CreateBitmaskUpTo(int bitCount) => BitMath.CreateBitmaskAt(bitCount) - 1;