So I have the following function:
static int calcDTSize( int depth )
{
if ( depth <= 8 )
{
return 1;
}
if ( depth <= 16 )
{
return 2;
}
if ( depth <= 32 )
{
return 4;
}
if ( depth <= 64 )
{
return 8;
}
throw std::exception( "Invalid bit count" );
}
Which calculates the size of datatype required for the specified number of bits. Originally I just had:
return ( (int) std::ceil( (double) depth / 8.0 ) );
However, on most machines that I know of, there isn’t a datatype which is 3 bytes long.
I’m sure there must be a neater way of doing the calculation without the if statements but I can’t think how.
Anyone got a better solution?
Divide by 8 and round up to closest power of 2.
Considering that the input is limited and the information is completely static, I would however probably put it in a look up array and do
If you don’t want 64 entries in the lut, you could do something like