Thanks for the pointer! I especially like (30|m^(m>>3)) - (((m^13)+1)>>4<<1, by “OMGWTF”; 30+(m&1^(m>7))-m*(m==2) by “byuu” (I’ve added parens to get it to parse correctly in Python) (and the nearly identical 30 + ((m + (m > 6)) % 2) - 2 * (m == 2) by “tlarkworthy”); and (m ^ (m >> 3) | 30) - 1 / (m * 9 % 17) * 2, by “recursive”. The various forms of 28 + ((0x3bbeecc >> m >> m) & 3) seem to be popular, and it’s clearly the most efficient solution by any measure, but it seems like it’s breaking the spirit of the thing.

I’m too lazy to do this, but I bet you’d get an interesting answer by making a Fourier approximation, since the months already alternate in a kind of obvious wave.

I won’t lie; as a programmer I find bit-twiddling unnaturally entertaining. Thanks for bringing this up.

The YCombinator News thread on this story has some fun alternate takes.

Thanks for the pointer! I especially like

`(30|m^(m>>3)) - (((m^13)+1)>>4<<1`

, by “OMGWTF”;`30+(m&1^(m>7))-m*(m==2)`

by “byuu” (I’ve added parens to get it to parse correctly in Python) (and the nearly identical`30 + ((m + (m > 6)) % 2) - 2 * (m == 2)`

by “tlarkworthy”); and`(m ^ (m >> 3) | 30) - 1 / (m * 9 % 17) * 2`

, by “recursive”. The various forms of`28 + ((0x3bbeecc >> m >> m) & 3)`

seem to be popular, and it’s clearly the most efficient solution by any measure, but it seems like it’s breaking the spirit of the thing.`(30|m^m>>3)-2*(m==2)`

is a combination of some of these that I found today that I particularly liked.This is cool. You could tell the same story starting with the knuckle thing too:

I’m too lazy to do this, but I bet you’d get an interesting answer by making a Fourier approximation, since the months already alternate in a kind of obvious wave.