I have a data stream that I get from a device. The chunks are 8 bytes, which I read as uint_64t
words. A few of the higher bits are flags (not all 4 bytes) that define the type of data. Some, but not all, of the chunks have the lower 4 bytes representing a float
in binary representation.
How do I correctly extract that part into a float
variable?
Let word be {4-byte flags, 4-byte float [LSB]}.
This "seems" to work:
float extracted = *reinterpret_cast<float *>(&word);
Yet, the compiler (GCC 10) with '-Wall' warns about type-punning
warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
for optimization levels >= -O2.
I suspect I'm doing all sorts of evil here and don't feel comfortable with the warning. What's the correct way doing this?
Thanks for your help!