private int RequiredBits(int v)
{
uint uv;
if (v >= 0)
{
uv = (uint)v;
}
else
{
uv = (uint)(-v);
}
// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
int r = 0;
for (int i = 4; i >= 0; i--)
{
if ((uv & LOG2_MASKS[i]) != 0)
{
uv >>= LOG2_POW[i];
r |= LOG2_POW[i];
}
}
r += 2;
return Math.Min(31, r);
}