Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created January 3, 2011 20:26
Show Gist options
  • Save juanplopes/763910 to your computer and use it in GitHub Desktop.
Save juanplopes/763910 to your computer and use it in GitHub Desktop.
public static int HighestBitPosition(ulong value)
{
if (value == 0) return -1;
var r = 0;
foreach (var e in new [] { 32, 16, 8, 4, 2, 1 })
if (value >= 1ul << e) { value >>= e; r += e; }
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment