Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created January 4, 2011 14:45
Show Gist options
  • Save juanplopes/764835 to your computer and use it in GitHub Desktop.
Save juanplopes/764835 to your computer and use it in GitHub Desktop.
static int[] index64 = new[]
{
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5
};
public static int BitScanForward(ulong value)
{
if (value == 0) return -1;
const ulong debruijn64 = 0x07EDD5E59A4E28C2ul;
return index64[((value & (~value + 1)) * debruijn64) >> 58];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment