Skip to content

Instantly share code, notes, and snippets.

@lvpidadiao
Created January 3, 2020 10:11
Show Gist options
  • Save lvpidadiao/902fcedb3dc1ff27690737be3bdbe367 to your computer and use it in GitHub Desktop.
Save lvpidadiao/902fcedb3dc1ff27690737be3bdbe367 to your computer and use it in GitHub Desktop.
floor or ceil power of 2
inline constexpr uint32_t ceilPowerOfTwo(uint32_t num) {
num |= (num >> 1);
num |= (num >> 2);
num |= (num >> 4);
num |= (num >> 8);
num |= (num >> 16);
return num - (num >> 1);
}
inline constexpr uint32_t roundupPowerOfTwo(uint32_t num) {
return ceilPowerOfTwo((num - 1) << 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment