bit_field: Left/right shifts are well defined in C++20

This commit is contained in:
Morph 2023-07-19 14:07:19 -04:00
parent 334567d33b
commit 7ad3e3c7bb
1 changed files with 4 additions and 0 deletions

View File

@ -175,6 +175,10 @@ constexpr OutputType ExtractBits(const RawType& raw) {
static_assert(sizeof(RawType) == sizeof(SignedType));
constexpr auto RightShift = BitSize<RawType>() - NumBits;
constexpr auto LeftShift = RightShift - Position;
// C++20: Signed Integers are Twos Complement
// Left-shift on signed integer types produces the same results as
// left-shift on the corresponding unsigned integer type.
// Right-shift is an arithmetic right shift which performs sign-extension.
return AutoBitCast<OutputType>((AutoBitCast<SignedType>(raw) << LeftShift) >> RightShift);
} else {
using UnsignedType = SmallestUIntType<BitSize<RawType>()>;