//glues bytes together, into 64bit
UINT64 AppendBytes64(uint8_t* val, int start)
{
    UINT64 result = 0;
    result = (val[start+0] << 0) | (val[start + 1] << 8) | (val[start + 2] << 16) | (val[start + 3] << 24) | (val[start + 4] << 32) | (val[start + 5] << 40) | (val[start + 6] << 48) | (val[start + 7] << 56);
    return result;
}因此,问题是visual studio不断警告算术溢出,左移位计数大于操作数大小,帮助。
发布于 2020-03-18 12:02:52
小积分类型的prvalues (如
char)可以转换为较大积分类型(如int)的prvalue。特别是,算术运算符不接受小于int的类型作为参数,如果适用,积分提升将在lvalue-rvalue转换之后自动应用。
为了避免隐式转换,您必须自己完成对适当类型的转换,并使用C++ 显式类型转换和无符号64位整数std::uint64_t的标准类型,以及用于索引的无符号类型std::size_t,应该如下所示:
#include <cstddef>
#include <cstdint>
using std::uint64_t;
constexpr uint64_t AppendBytes64(const uint8_t* val, std::size_t start) {
    return static_cast<uint64_t>(val[start + 0]) << 0 | 
           static_cast<uint64_t>(val[start + 1]) << 8 | 
           static_cast<uint64_t>(val[start + 2]) << 16 |
           static_cast<uint64_t>(val[start + 3]) << 24 |
           static_cast<uint64_t>(val[start + 4]) << 32 |
           static_cast<uint64_t>(val[start + 5]) << 40 |
           static_cast<uint64_t>(val[start + 6]) << 48 |
           static_cast<uint64_t>(val[start + 7]) << 56;
}constexpr使使用常量表达式中的函数从C++11和转发中获得编译时间常数(给定的constexpr输入)成为可能。
发布于 2020-03-18 11:42:32
在移动之前键入val到UINT64。
result = ((UINT64)val[start + 0] << 0) | ((UINT64)val[start + 1] << 8) | ((UINT64)val[start + 2] << 16) | ((UINT64)val[start + 3] << 24) | ((UINT64)val[start + 4] << 32) | ((UINT64)val[start + 5] << 40) | ((UINT64)val[start + 6] << 48) | ((UINT64)val[start + 7] << 56);发布于 2020-03-18 11:42:23
在shift表达式中,使用了uint8_t类型的操作数到int类型的整数提升。必须将shift表达式中的操作数转换为UINT64类型。
https://stackoverflow.com/questions/60738694
复制相似问题