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.

15 lines
561 B

  1. #pragma once
  2. inline size_t smallestIntWithNBitsSet(size_t n) {
  3. static_assert(sizeof(size_t) == 8, "size_t has wrong size.");
  4. STORM_LOG_ASSERT(n < 64, "Input is too large."); // TODO fix this for 32 bit architectures!
  5. if(n==0) return static_cast<size_t>(0);
  6. return (1 << n) - 1;
  7. }
  8. inline size_t nextBitPermutation(size_t v) {
  9. if (v==0) return static_cast<size_t>(0);
  10. // From https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation
  11. size_t t = (v | (v - 1)) + 1;
  12. return t | ((((t & -t) / (v & -v)) >> 1) - 1);
  13. }