Search code examples
c++bytebit-shift

Warning: left shift count >= width of type when reading bytestream into double variable in C++


I'm trying to read a bytestream into a double variable in C++. So my code for this is the following:

double foo;
foo = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24) | (bytes[44] << 32) | (bytes[5] << 40) | (bytes[6] << 48) | (bytes[7] << 56);

As you can see I'm trying to read in 64 bits. And even though double isn't a fixed size, it should be 64 bits on nearly any machine. (sizeof(double) gives me 8 bytes as size)

But still I get this warning:

Warning: left shift count >= width of type

Can I just ignore that warning - or can I somehow make the size of the double fixed (as I know there is no sizefixed floating point datatype in C/C++)?

Thanks for you help!


Solution

  • I found out a solution for myself - but thanks to all the comments!

    I tried memcpy() and that worked fine.

    I solved it like this:

    foo = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24) | ((uint64_t)bytes[44] << 32) | ((uint64_t)bytes[5] << 40) | ((uint64_t)bytes[6] << 48) | ((uint64_t)bytes[7] << 56);