In the above example, the last entry has value of 10, meaning I need at least 4 bits to write it (assuming only positive values). I can calculate this value in runtime via a function, but it would a bit nicer to have this value at compile time so I could do this:
I'm saving enum values to bit stream, and I would like to use only such number of bits as really required.
Any ideas?
Quit wasting your time on the triviality and work on making more/better code. Realistically you're likely to add more enum values over time; leave room for them.
Quit wasting your time on the triviality and work on making more/better code. Realistically you're likely to add more enum values over time; leave room for them.
I asked a specific question, trivial or not. And I can waste my time any way I want.
Who said anything about storage? Believe it or not, there are still some parts of the world where network bandwidth is not abundant.
but you could use one bit flag to tell whether to read a full byte or 4bits
I'm not sure how this would help me. I want to save minimum number of bits required. It could be 2, 4, 7 or even 23. I just wanted to know, if anybody had a quick idea how to get number of minimum bits required based on some max value (at compile time).
Set a constant with the correct number. There is (to my knowledge) no great way to do that programmatically. There is likely template black magic, but I don't know of any offhand. You can parse out the file and do code generation, but that sucks.
A very trivial way to get the number of necessary bits to store a value at compile time:
[source]
template<unsigned int N> struct number_of_bits
{
static const unsigned int value = 1 + number_of_bits<(N >> 1)>::value;
};
template<> struct number_of_bits<0>
{
static const unsigned int value = 0;
};
[/source]
Use:
[source]
int bits = number_of_bits<42>::value;
[/source]
It's not very generic or error-safe, but it works under normal circumstances at least. Add whatever error-checking you feel is necessary, or modify to suite you needs.
[quote name='vNeeki' timestamp='1330446235' post='4917415']
HDD/RAM space is not a problem these days
Who said anything about storage? Believe it or not, there are still some parts of the world where network bandwidth is not abundant.
but you could use one bit flag to tell whether to read a full byte or 4bits
I'm not sure how this would help me. I want to save minimum number of bits required. It could be 2, 4, 7 or even 23. I just wanted to know, if anybody had a quick idea how to get number of minimum bits required based on some max value (at compile time).
[/quote]
A very trivial way to get the number of necessary bits to store a value at compile time:
[source]
template<unsigned int N> struct number_of_bits
{
static const unsigned int value = 1 + number_of_bits<(N >> 1)>::value;
};
template<> struct number_of_bits<0>
{
static const unsigned int value = 0;
};
[/source]
Use:
[source]
int bits = number_of_bits<42>::value;
[/source]
It's not very generic or error-safe, but it works under normal circumstances at least. Add whatever error-checking you feel is necessary, or modify to suite you needs.