警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度

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

本文关键字:双精度 读取 字节流 变量 类型 左移 gt C++ 警告      更新时间:2023-10-16

我正在尝试将字节流读入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 个字节作为大小(

但我仍然收到此警告:

警告:左移计数>= 字体宽度

我可以忽略该警告 - 或者我可以以某种方式使双精度固定的大小固定(因为我知道 C/C++ 中没有 sizefixed 浮点数据类型(?

感谢您的帮助!

我为自己找到了解决方案-但感谢所有评论!

我试了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);