Jul 8 2008
21:56 this is freakin ballin. i was so happy to see this!
Jul 10 2008
14:25 jctoast@hotmail.com
Jul 11 2008
05:31 jjjj
Jul 16 2008
14:50 Click to yabber
Jul 18 2008
20:17 Click to jabber
Jul 23 2008
14:02 how do you freaking get to the open door on 64
Jul 27 2008
23:53 awesome
Aug 5 2008
01:53 YABBERTIEM
Aug 10 2008
11:10 i wanna put this on my myspace, any clues
Aug 14 2008
22:33 <Jabber>
Aug 18 2008
07:21 Click to yabber
07:21 malllll
07:21 שלום שלום
23:10 <Jabber>
Aug 19 2008
06:18 helooo
Aug 20 2008
02:57 click to yabber

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]