[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.
Someone did a similar thing on a previous International Obfuscated C Code Contest: http://www.ioccc.org/2004/vik2.hint
Why? I mean if you do it with gates… But using C? You are using thousands of gates to simulate a handful of them and doing it quite slowly to boot? Why?
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.
Other people use millions of gates to play a silly game. This person is actually learning something.
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 :)
But why is this a “hack”? It is not. Should not be in HaD.
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.
Why do people post comments like this? Do they literally never do something that isn’t somehow directly useful? I bet they’re loads of fun at parties.
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.
I don’t see any preprocessor tricks… Try ## very powerfull tricks.
He is using ##.
+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.
@ Acid please ensure all of your posts are full of assembly and NASA grade programming.
Your post is not written in encrypted binary, I am disappointed!
Get over yourself. I will wait with baited breath for the projects that YOU’VE done that have made it to the internet.
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 instructables.com) that don’t make it to HaD. Why this one made it is surprising… I think HaD needs a bit more editorial work.
I’d like to see a post on how not to make stupid comments on other posts. It might not be strictly appropriate for HaD, but it might help.
Many years ago I did full adder in sed. You could add arbitrary length binary numbers. :)
Full adder in sed:
http://www.emsi.it.pl/SED/fadder.sed
The less obfuscated version that runs every logic gate separately:
http://www.emsi.it.pl/SED/full_add.sed
:)
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 (https://github.com/liuliu/ccv/blob/unstable/lib/ccv.h)
…
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)
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…
+1000. To me, HaD is to show what other, like-minded people are doing, either to inspire me, or to let me learn something from this experience. This does that, hence it belongs.
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.
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 :)
Good Work. You might should surround all the uses of parameters in parentheses as described in the following stack overflow answer: http://stackoverflow.com/questions/10820340/the-need-for-parentheses-in-macros-in-c
I thought Cleopatra showed that you can’t have a full adder unless you let it byte continually. Some would puff occasionally, particularly during overflow conditions.