CodeSOD: It's Log, Log, Log
by Ellis Morning from The Daily WTF on (#1NGKR)
Reader Bernie submits for our approval this wonderful C# log base 2 implementation. He says: "We can distinguish two halves in that code. It looks like it was originally written for an unsigned 16-bit int, and later on extended for signed 32-bit integers."
This code reminds me of the change-sorting machine I have in my closet, which sorts change based on weight. That thing has no error handling if you try to sort, say, a huge boulder through it; it'd just fall apart. At least here, we have the assurance that the universe's biggest numbers all have a log base 2 of 31.
public static int Log2(int x) { if (x[Advertisement] Release!is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped!