Rozważmy następujący kod:Czy operacje bitowe na podpisanych typach całek są dobrze zdefiniowane?
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;
Zastanawiam się, jak dobrze te operacje są zdefiniowane zgodnie z normą. Czy mam gwarancję, że otrzymam takie same wyniki na wszystkich architekturach? Na pewno działam na bicie znaku na wszystkich architekturach, gdzie ten bit znaku jest 0
dla liczb dodatnich i 1
dla liczb ujemnych?
Zobacz także [Jaki jest wynik b & b] (http://stackoverflow.com/q/29394518/1708801), który jest blisko spokrewniony, ale nie jest duplikatem. –