I have an interesting little problem, and I know there are multiple ways to skin the cat but I was wondering what would the best/most efficient way be.
Say for example I have an integer with the value 534 and an array that can store 16 booleans
now, 534 to binary is 10000010110
how would be the best way to get from 534 to
array[0] = 0
array[1] = 1
array[2] = 1
array[3] = 0
array[4] = 1
....
array[15] = 0
thanks in advance!
Use
std::bitset<16>and calloperator[]to access individual bits:Output (demo):
This may not be most efficient but if you consider safety, then it is better alternative to raw array types. The efficiency difference will be almost negligible.
If the number of bits is not known at compile time, and can be known at runtime, then
boost::dynamic_bitsetwill help you. Have a look at it:From its doc,