I'm trying to convert any object to std::vector<bool>
(representing bits set in memory to strore the object).
So, for uint16_t(0x1234)
, I want to get std::vector<bool>
0001001000110100
. And for float(3.14f)
, which is 0x4048F5C3
in memory, I want to get std::vector<bool>
01000000010010001111010111000011
.
Based on this post, I tried this:
template<typename T>
std::vector<bool> val2Array(T val)
{
size_t count = sizeof(T)*CHAR_BIT;
std::vector<bool> result( count );
for (size_t i = 0; i < count; i++)
{
T temp = 1 << (count - i - 1);
if ( val & temp )
result[i] = true;
else
result[i] = false;
}
return result;
}
This works fine, but I get an error when T
is not numerical (float
), due to <<
operator.
Is there any other way to convert any value to a std::vector<bool>
? I found lots of code showing how to convert std::vector<bool>
to a value but not the way around.
Aucun commentaire:
Enregistrer un commentaire