Skip to content

Instantly share code, notes, and snippets.

@mjf
Created February 4, 2014 01:18
Show Gist options
  • Save mjf/8795784 to your computer and use it in GitHub Desktop.
Save mjf/8795784 to your computer and use it in GitHub Desktop.
Find next power of two in C
static size_t
npow2(size_t n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment