考慮以下代碼:有符號整型類型按位運算的結果是否定義良好?
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
(負數)?
另請參閱[a和b的結果是什麼?](http://stackoverflow.com/q/29394518/1708801),它與密切相關但不重複。 –