Skip to content

Instantly share code, notes, and snippets.

View kusma's full-sized avatar

Erik Faye-Lund kusma

View GitHub Profile
@kusma
kusma / mag.cpp
Last active January 31, 2017 13:47
compile-time calculate the magnitude (the number of bits needed to store a number) of a given number
template <unsigned int n, unsigned int bit = 31>
struct mag
{
enum {
lval = (n & (1UL << bit)) != 0 ? bit : 0,
rval = mag<n, bit - 1>::value,
value = lval > rval ? lval : rval // max
};
};