public static (byte sum, byte carry) Add(byte a, byte b) { byte result = 0; byte carry = 0; for (int i = 0; i < 8; i++) { var bitsum = BitAdder.Add((byte)(a >> i & 1), (byte)(b >> i & 1)); var adjBitsum = BitAdder.Add(bitsum.sum, carry); carry = BitAdder.Add(bitsum.carry, adjBitsum.carry).sum; result = (byte)(result | (adjBitsum.sum << i)); } return(result, carry); }
static string bitAdd(byte a, byte b) { var ret = BitAdder.Add(a, b); return(string.Format("{0} + {1} = {2}", a, b, (ret.carry > 0 ? "1" : " ") + ret.sum)); }