It wasn't for a serious program, but i had some really weird C code written for the old code golf competitions.
Back in the Game Of Life one, I decided I would write my entry for the GBA. Normally in the GoL, you'd have to implement a swap buffer. The front buffer would hold the previous GoL state while the program used it to build the next state in the back buffer. Then at the end of the frame, the buffers would be flipped and the new front buffer copied to the screen. This all ends up being a good chunk of code. And for Code Golf, that's obviously bad.
However, the GBA has a screen mode which splits video memory into two segments. On any one frame, one segment would be used for drawing the next frame while the other segment is displayed... Sound familiar? So in my GoL entry, I [ab]used video memory to serve as my GoL buffer. I read directly from pixels on the screen for my previous GoL state and read directly to the video backbuffer to generate my next state. Than I simply swap the buffers to increment the GoL sim and to display all in one.
Another weird thing I did was in a previous Code Golf where you had to read your own source file. As part of the program, I needed a concise way to check if I hit the end of the file. Normally, you'd do a comparrison against EOF like so:
This is waaay too verbose, though. For starters you have to duplicate i = getc(f);!
You can cut it down if you remember that in C, assignments have an implicit return value (so you can do things like a = b = c)
This turned the code into:
Pretty good, right? But still not enough. Precedence rules make it so you have to waste two characters on parenthesis. I also wanted to get rid of the EOF comparison wholesale.
In the C standard, EOF is always -1. So by that, you can always add one to get 0, which is the value for false in C. Unfortunately, because we're changing every character that gets read, we have to revert it when we want to use it.
However, this is still an extra 4 characters. I wanted to cut that down even further. It just so happens that -1 is 0xFFFFFFFF. Or all 1's in binary. So all I really needed to do was flip all bits and if the value was -1, it'll become 0. Fortunately, C has a bitwise operator for that: ~. So my final code became:
Back in the Game Of Life one, I decided I would write my entry for the GBA. Normally in the GoL, you'd have to implement a swap buffer. The front buffer would hold the previous GoL state while the program used it to build the next state in the back buffer. Then at the end of the frame, the buffers would be flipped and the new front buffer copied to the screen. This all ends up being a good chunk of code. And for Code Golf, that's obviously bad.
However, the GBA has a screen mode which splits video memory into two segments. On any one frame, one segment would be used for drawing the next frame while the other segment is displayed... Sound familiar? So in my GoL entry, I [ab]used video memory to serve as my GoL buffer. I read directly from pixels on the screen for my previous GoL state and read directly to the video backbuffer to generate my next state. Than I simply swap the buffers to increment the GoL sim and to display all in one.
Another weird thing I did was in a previous Code Golf where you had to read your own source file. As part of the program, I needed a concise way to check if I hit the end of the file. Normally, you'd do a comparrison against EOF like so:
Code Select
i = getc(f);
while(i != EOF){
//...
i = getc(f);
}
This is waaay too verbose, though. For starters you have to duplicate i = getc(f);!
You can cut it down if you remember that in C, assignments have an implicit return value (so you can do things like a = b = c)
This turned the code into:
Code Select
while((i = getc(f)) != EOF)
//...
Pretty good, right? But still not enough. Precedence rules make it so you have to waste two characters on parenthesis. I also wanted to get rid of the EOF comparison wholesale.
In the C standard, EOF is always -1. So by that, you can always add one to get 0, which is the value for false in C. Unfortunately, because we're changing every character that gets read, we have to revert it when we want to use it.
Code Select
while(i = getc(f)+1) a[i-1]++;
However, this is still an extra 4 characters. I wanted to cut that down even further. It just so happens that -1 is 0xFFFFFFFF. Or all 1's in binary. So all I really needed to do was flip all bits and if the value was -1, it'll become 0. Fortunately, C has a bitwise operator for that: ~. So my final code became:
Code Select
while(i=~getc(f))a[~i]++;