Skip to content

Instantly share code, notes, and snippets.

@frangio
Created September 24, 2022 04:19
Show Gist options
  • Save frangio/74dc44635aa99cc603e9d6f048e54e87 to your computer and use it in GitHub Desktop.
Save frangio/74dc44635aa99cc603e9d6f048e54e87 to your computer and use it in GitHub Desktop.
function nlz(uint x) pure returns (uint n) {
uint y;
n = 256;
y = x >> 128; if (y != 0) { n = n - 128; x = y; }
y = x >> 64; if (y != 0) { n = n - 64; x = y; }
y = x >> 32; if (y != 0) { n = n - 32; x = y; }
y = x >> 16; if (y != 0) { n = n - 16; x = y; }
y = x >> 8; if (y != 0) { n = n - 8; x = y; }
y = x >> 4; if (y != 0) { n = n - 4; x = y; }
y = x >> 2; if (y != 0) { n = n - 2; x = y; }
y = x >> 1; if (y != 0) return n - 2;
return n - x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment