Last active
December 15, 2015 15:29
-
-
Save TechplexEngineer/5281928 to your computer and use it in GitHub Desktop.
64bit count leading 0's in c
rev2: added attribution, fixed comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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