BitStream
bits.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <cstdint>
5 
7 {
8  constexpr inline uint32_t bits_to_represent(uintmax_t n)
9  {
10  uint32_t r = 0;
11 
12  if (n >> 32) { r += 32U; n >>= 32U; }
13  if (n >> 16) { r += 16U; n >>= 16U; }
14  if (n >> 8) { r += 8U; n >>= 8U; }
15  if (n >> 4) { r += 4U; n >>= 4U; }
16  if (n >> 2) { r += 2U; n >>= 2U; }
17  if (n >> 1) { r += 1U; n >>= 1U; }
18 
19  return r + static_cast<uint32_t>(n);
20  }
21 
22  constexpr inline uint32_t bits_in_range(intmax_t min, intmax_t max)
23  {
24  return bits_to_represent(static_cast<uintmax_t>(max) - static_cast<uintmax_t>(min));
25  }
26 }
Definition: bits.h:7
constexpr uint32_t bits_in_range(intmax_t min, intmax_t max)
Definition: bits.h:22
constexpr uint32_t bits_to_represent(uintmax_t n)
Definition: bits.h:8