two-bit/bit.ts

22 lines
456 B
TypeScript

type BinaryDigit = 0 | 1
export interface Bit {
value: BinaryDigit
add: (other: Bit) => { ones: Bit, carry: Bit }
}
export default function bit(value: BinaryDigit): Bit {
function add(other: Bit) {
const result = (value + other.value)
const ones = result % 2 as BinaryDigit
const carry = result === 2 ? 1 : 0
return { bit: bit(ones), carry: bit(carry) }
}
return {
add,
value
}
}