Create A Full Adder Using The C Preprocessor


[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 ) )

This preprocessor code above defines the traditional full adder logic in terms of AND and XOR. That’s really all there is to the logic of the adder itself. The next step is to chain the adders together for four bits. More bits are possible, but the code would get rather messy.

The last bit of magic [Phillip] performed was to convert decimal numbers to binary. He used the same concatenation trick as he did with XOR and AND to make this happen. The end result is a program that can take two numbers as preprocessor options, and compile a binary that will always add those same two numbers when it is run. Like we said – not very useful itself, but a great way to learn some tricks with the C preprocessor that definitely will come in handy down the road.

26 thoughts on “Create A Full Adder Using The C Preprocessor

    1. Sure it’s using a ton more gates than a true hardware adder – but that’s not the point. Phillip created the this as a “learn by doing” project for the C Preprocessor.

    2. I did it as an experiment; it doesn’t have much practical use (Boost Preprocessor implements their addition differently), but it was a nice way to spend a few hours playing with one of the more curious aspects of the C preprocessor :)

        1. I found it interesting. You had the option of skipping the article, but you not only opened it, you commented on it. Therefore, you were interested too, even if you claim otherwise. Hence, you actually believe this belongs on HaD. I agree with your deobfuscated conclusion entirely.

    3. I do not see the point either. Yes, it is a learning exercise. Quite elementary. But why is it given prominence in HaD? Are any and all simple things like this considered a “hack” these days? Sad.

  1. +1 @Json
    If HAD continues down that road, next year we’ll be learning how to do that magic trick with the endless stream of HELLO! using just a monitor, a PC, a mouse, a keyboard and an internet connection.

      1. Precisely. This one should NOT have made it to the internet, not as a “HACK”. Perhaps as a basic tutorial on C or on combinatorial logic. There are lots of interesting hacks out there (I particularly like that don’t make it to HaD. Why this one made it is surprising… I think HaD needs a bit more editorial work.

  2. Recently I’ve found another interesting use for pre-processor macros: function overloading (which will be impossible in plain C). I found that in the CCV project (

    int ccv_read_impl(const void* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline);
    #define ccv_read_n(in, x, type, rows, cols, scanline, …) \
    ccv_read_impl(in, x, type, rows, cols, scanline)
    #define ccv_read(in, x, type, …) \
    ccv_read_n(in, x, type, ##__VA_ARGS__, 0, 0, 0)
    // this is a way to implement function-signature based dispatch, you can call either
    // ccv_read(in, x, type) or ccv_read(in, x, type, rows, cols, scanline)

  3. Phillip.. Don’t listen to anyone giving negative comments on here. There are allot of negativity trolls around. Personally I like to see post about people learning how things work, how to do things, etc.

    Although I don’t post hacks, I have done my fair share of making things work that shouldn’t be working, from computers, to av equipment, to my own hud I’m developing, etc. I don’t do it for the hacks, I do it to learn and to push myself.. This is part of what mods/hacking/etc are about, the learning….. Let’s not forget this…

    1. I fully agree with you Augur and vpoko. A good hack does not have to be a practical application. It does not have to be a real piece of hardware either. A good hack teaches something to someone. This hack did that for me, for you, and probably for the trolls too.

    2. Hey, I’m hardly concerned; I’ve been on HaD for years. I’d like to think it’s a hack because it’s an interesting, unusual application of the C preprocessor, but people will believe what they want to believe :)

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.