You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516
  1. // Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package utils
  4. // return n such that v <= n and n == 2**i for some i
  5. func RoundUpToPowerOfTwo(v int) int {
  6. // http://graphics.stanford.edu/~seander/bithacks.html
  7. v -= 1
  8. v |= v >> 1
  9. v |= v >> 2
  10. v |= v >> 4
  11. v |= v >> 8
  12. v |= v >> 16
  13. return v + 1
  14. }