Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Last active December 15, 2015 15:29
Show Gist options
  • Save TechplexEngineer/5281928 to your computer and use it in GitHub Desktop.
Save TechplexEngineer/5281928 to your computer and use it in GitHub Desktop.
64bit count leading 0's in c rev2: added attribution, fixed comments
//Count Leading Zeros (CLZ)
//Adapted from Wikipedia: http://en.wikipedia.org/wiki/Find_first_set#Algorithms
int clz (uint64_t x) {
uint64_t t;
int r;
if (x == 0)
return 0;
t = 0x8000000000000000; //mask
r = 0; //number of zeros counter
while ((x & t) == 0 && r < 64) {
t = t >> 0x1;
r ++;
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment