I\'m looking for the most efficient way to calculate the minimum number of bytes needed to store an integer without losing precision.
e.g.
int: 10 = 1 byte
For each of eight times, shift the int eight bits to the right and see if there are still 1
-bits left. The number of times you shift before you stop is the number of bytes you need.
More succinctly, the minimum number of bytes you need is ceil(min_bits/8)
, where min_bits
is the index (i+1)
of the highest set bit.