Jun 16 2008
08:20 gggg
19:00 lmfao i cant believe this ultimate game is still around
23:10 rawer?
Jun 17 2008
04:39 any one do lvl 18?
15:14 irc
15:15 wapirc
Jun 18 2008
08:02 level 18 is almost impossible with the lag i get from this website, i cant beat that level now but i used to be able to beat it when i was about 8 years old on a computer back in 1995. um screw lag
10:10 The "goes toward" operator is SO cute
10:15 Best part of MS 'optimisation' is that __inline attribute
11:57 actually, , the bombs explode twice as fast in this version. it's a bug :-)
Jun 19 2008
13:16 sfd
Jun 20 2008
10:19 click to yabber
Jun 29 2008
02:58 Where's the MUSIC??!!11!?
03:00 I cluck too soon. Any chance I can get a copy of this thing, and maybe mynephewthegeek can bolt the music back on? That was half the game...
Jun 30 2008
15:51 <Jabber>
16:36 You can get a copy over at home-of-the-underdogs, ; it runs well in DOSBOX

steike / code / useless / evil c

This is a collection of strange C (and some Java) constructs. It's probably best not to use them, but you should know why they work.

The cast-to-bool operator

        Node *left, *right;
        int childCount() {
                return !!left + !!right;
        }

Logical XOR

A variation on the above. (Note that ^ is bitwise XOR, not logical XOR; 3 ^ 4 does not give 0.)

        int xor(int a, int b) {
                return !a != !b;
        }

The "goes toward" operator

        void doStuff(int count) {
                while(count --> 0) 
                        fleh();
        }

Useless but pretty definitions of BOOLs

        #define TRUE  '/'/'/'
        #define FALSE '-'-'-'

Two (2) readers wrote in to comment that the above is unsafe due to operator precedence.

Yes.

That is true.

If you are doing arithmetic with the above, please seek help.

Optimization, the Microsoft way

This was found in the 2004 leaked Windows sources

__inline BOOL
SearchOneDirectory(
                  IN  LPSTR Directory,
                  IN  LPSTR FileToFind,
                  IN  LPSTR SourceFullName,
                  IN  LPSTR SourceFilePart,
                  OUT PBOOL FoundInTree
                  )
{
    //
    // This was way too slow. Just say we didn't find the file.
    //
    *FoundInTree = FALSE;
    return(TRUE);
}

The canonical temp-less swap

As about a dozen people have reminded me over the last 24 hours, and as this page used to say, this is not well-defined nowadays.

        a^=b^=a^=b;

Duff's Device for loop unrolling

        switch(c&3) while((c-=4)>=0) {
                foo(); case 3:
                foo(); case 2:
                foo(); case 1:
                foo(); case 0:
        }

Struct/class offsets

        int ofs = (int)&((Class*)0)->element;

[Vilhelm S. comments: Note that <stddef.h> provides an offsetof(type, field_name) macro, so you can leave the dirty work of abusing NULL pointers in perverted ways to your standard library implementor! (The typical implementation of it is as above, though...)]

Fun with comments

This font definition has a bug. Find it.

char font[] = {
....
0x000,0x061,0x051,0x049,0x045,0x043,0x000,0x000, // 1 :0x5A Z
0x070,0x029,0x024,0x022,0x024,0x029,0x070,0x000, // 1 :0x5B [
0x000,0x03D,0x042,0x042,0x042,0x042,0x03D,0x000, // 1 :0x5C \
0x000,0x03C,0x041,0x040,0x041,0x040,0x03C,0x000, // 1 :0x5D ]
...

Endian magic

This is just plain evil.

        // this is from GMP
        #define BYTE_OFS(x) (((char *)&internalEndianMagic)[7-(x)])
        static double internalEndianMagic = 7.949928895127363e-275;

Yes, it works by exploiting the exact IEEE binary representation of that constant.

Another micro-optimization

"If x or y is less than 0" :-) Works with and and xor as well

        if((x|y) < 0) { ... }

Thankfully, gcc -- usually the lowest common denominator among compilers -- implements this optimization. So it's all right to beat your minion programmers with a stick when they do this by hand. -- benoit hudson

Lispy with-foo-bound-to

#define within(obj) for(QWidget *__t = (obj), *parent = __t; parent; parent=0)

within(this) {
  within(new QSplitter(parent)) {
     within(new QVBoxWidget(parent)) { 
       ...
       ...
     }
     within(...) {
  ...

Dan's lexer tester

/* Test whether compiler supports C++-style comments */
#define HELPER 0//**/ 
#define CPLUSPLUS_COMMENTS_SUPPORTED (HELPER+1)

[] is symmetric

thanks sigfpe

int direction = 1;
char direction_name = direction["nsew"];

Walsh-Moler-Newton inverse square root

Thanks captainfwiffo, gsg and Anders; this one is actually useful (and thus shouldn't really be on this page) but makes up for it by pure IEEE abuse. History here.

float Q_rsqrt( float number )
{
  long i;
  float x2, y;
  const float threehalfs = 1.5F;

  x2 = number * 0.5F;
  y  = number;
  i  = * ( long * ) &y;                       // grab bits
  i  = 0x5f3759df - ( i >> 1 );               // do magic
  y  = * ( float * ) &i;
  y  = y * ( threehalfs - ( x2 * y * y ) );   // improve

  return y * number;
}

snoob

This one is for mrkite. Given an integer with n bits set, it will return the next higher number with the same number of bits set. From Hacker's Delight.

unsigned snoob(unsigned x) {
   unsigned smallest, ripple, ones;
                                // x = xxx0 1111 0000
   smallest = x & -x;           //     0000 0001 0000
   ripple = x + smallest;       //     xxx1 0000 0000
   ones = x ^ ripple;           //     0001 1111 0000
   ones = (ones >> 2)/smallest; //     0000 0000 0111
   return ripple | ones;        //     xxx1 0000 0111
}

List comprehensions, Java style

return new ArrayList<String>() {{ for(String x : foo) add(x.toUpper(); }};

[Comment on this]