Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- constexpr unsigned BitsRequired(T min, T max)
- {
- template <unsigned x> struct PopCount
- {
- enum {
- a = x - ((x >> 1) & 0x55555555),
- b = (((a >> 2) & 0x33333333) + (a & 0x33333333)),
- c = (((b >> 4) + b) & 0x0f0f0f0f),
- d = c + (c >> 8),
- e = d + (d >> 16),
- result = e & 0x0000003f
- };
- };
- template <unsigned x> struct Log2
- {
- enum {
- a = x | (x >> 1),
- b = a | (a >> 2),
- c = b | (b >> 4),
- d = c | (c >> 8),
- e = d | (d >> 16),
- f = e >> 1,
- result = PopCount<f>::result
- };
- };
- return (min == max) ? 0 : (Log2<uint32_t(max - min)>::result + 1);
- }
Add Comment
Please, Sign In to add comment