警告:在 C++ 中将字节流读入 double 变量时左移计数 >= 类型宽度
Warning: left shift count >= width of type when reading bytestream into double variable in C++
我正在尝试将字节流读入 C++ 中的双精度变量。
所以我的代码如下:
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);
如您所见,我正在尝试以 64 位读取。即使 double 不是固定大小,它在几乎任何机器上都应该是 64 位。 (sizeof(double)
大小为 8 个字节)
但我仍然收到此警告:
Warning: left shift count >= width of type
我能否忽略该警告 - 或者我能否以某种方式固定双精度数的大小(据我所知,C/C++ 中没有固定大小的浮点数据类型)?
感谢您的帮助!
我找到了自己的解决方案 - 但感谢所有评论!
我尝试了 memcpy()
,效果很好。
我是这样解决的:
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);
我正在尝试将字节流读入 C++ 中的双精度变量。 所以我的代码如下:
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);
如您所见,我正在尝试以 64 位读取。即使 double 不是固定大小,它在几乎任何机器上都应该是 64 位。 (sizeof(double)
大小为 8 个字节)
但我仍然收到此警告:
Warning: left shift count >= width of type
我能否忽略该警告 - 或者我能否以某种方式固定双精度数的大小(据我所知,C/C++ 中没有固定大小的浮点数据类型)?
感谢您的帮助!
我找到了自己的解决方案 - 但感谢所有评论!
我尝试了 memcpy()
,效果很好。
我是这样解决的:
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);