【发布时间】:2016-01-19 23:39:17
【问题描述】:
考虑这段代码:
using integer = int; // or any other fundamental integral type
using unsigned_integer = typename std::make_unsigned<integer>::type;
constexpr integer bits = std::numeric_limits<unsigned_integer>::digits;
integer value = -42; // or any value
integer mask = static_cast<integer>(1)<<static_cast<integer>(bits-1);
bool result_and = value & mask;
bool result_or = value | mask;
bool result_xor = value ^ mask;
我想知道根据标准定义这些操作的效果如何。我能保证在所有架构上都得到相同的结果吗?我肯定会在所有体系结构上对符号位进行操作,其中符号位为 0 用于正数,1 用于负数?
【问题讨论】:
-
另见What's the result of a & b? 密切相关但不重复。
标签: c++ bit-manipulation standards c++14 signed