most mathematical operations of a computer involve addition on some level - subtraction becomes the addition of numbers in two's complement, multiplication repetitive addition, and division repetitive subtraction. here's a small set of classes that add binary bits just like a computer:
Code:
namespace adder {
struct result {
result & set(bool _sum, bool _carry) {
sum = _sum, carry = _carry;
return *this;
}
void clear(void) {
set(0, 0);
}
result(bool _sum = 0, bool _carry = 0) {
set(_sum, _carry);
}
bool sum, carry;
};
struct halfadder : result {
result & add(bool a, bool b) {
sum = xor_gate(a, b);
carry = and_gate(a, b);
return *this;
}
halfadder(bool a = 0, bool b = 0) {
add(a, b);
}
};
struct fulladder : halfadder {
result & add(bool a, bool b, bool carry_in) {
halfadder first(a, b);
halfadder::add(carry_in, first.sum);
carry = or_gate(carry, first.carry);
return *this;
}
fulladder(bool a = 0, bool b = 0, bool carry_in = 0) {
add(a, b, carry_in);
}
};
} // namespace adder
good luck!