1. 21
  1.  

  2. 8

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

    1. 3

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

      1. 1

        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.

        1. 1

          (30|m^m>>3)-2*(m==2) is a combination of some of these that I found today that I particularly liked.

      2. 2

        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.

        1. 2

          This is cool. You could tell the same story starting with the knuckle thing too:

          • Months have 30 days, or you have flabby hands with no knuckles (y = 30)
          • Alternating months have 31, or you have a knuckle to start, then a gap then a knuckle and so on (y = 30 + x % 2)
          • Except they don’t, something weird happens after the x = 7, or the fourth knuckle, and the process gets offset by one (y = 30 + (x + floor(x/8)) % 2)
          • The first gap (February) is extra weird. The article’s trick of using 2 % x + 2 * floor(1/x) is clever.