[Phillip] wanted to play with the C preprocessor. He decided to do that by creating a 4 bit full adder. We know this is pretty useless in everyday life, but it was a great learning experience. The beauty of this adder is right at the start. [Phillip] defines truth tables for XOR and AND. He’s able to then create strings that reference these truth tables.
For example: the first line of [Phillip’s] AND table is #define AND_00 0. If the preprocessor concatenates strings that equal “AND_00” they will then be converted to 0. This is the groundwork for the half adder .
The next step is the operational logic, which of course falls upon macros:
/* Full adder macros */ /* Out = (A ^ B) ^ cin */ #define FULL_ADD_OUT( a, b, cin ) \ XOR( XOR( a, b ), cin ) /* Carry_out = (A & B) ^ (Carry_in & (A ^ B)) */ /* The standard adder uses OR for the last 'gate' - this can safely be changed to XOR, which has been done here to avoid defining an OR operator */ #define FULL_ADD_CARRY( a, b, cin ) \ XOR( AND( XOR( a, b ), cin ), AND( a, b ) )
Continue reading “Create A Full Adder Using The C Preprocessor”