Skip to content

Instantly share code, notes, and snippets.

@kimushu
Last active January 2, 2016 22:39
Show Gist options
  • Save kimushu/8371723 to your computer and use it in GitHub Desktop.
Save kimushu/8371723 to your computer and use it in GitHub Desktop.
Calculate a ceiling 'power of 2' value without assembler or loop
static inline uint32_t calc_power_of_2(uint32_t n)
{
--n;
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
++n;
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment