diff --git a/rx/include/rx/bits.hpp b/rx/include/rx/bits.hpp index c2762efd9..5fab46ebb 100644 --- a/rx/include/rx/bits.hpp +++ b/rx/include/rx/bits.hpp @@ -6,7 +6,21 @@ inline constexpr T getBits(T value, unsigned end, unsigned begin) { return (value >> begin) & ((1ull << (end - begin + 1)) - 1); } +template +inline constexpr T setBits(T value, unsigned end, unsigned begin, U bits) { + auto mask = ((1ull << (end - begin + 1)) - 1) << begin; + return static_cast((value & ~mask) | + ((static_cast(bits) << begin) & mask)); +} + template inline constexpr T getBit(T value, unsigned bit) { return (value >> bit) & 1; } +template +inline constexpr T setBit(T value, unsigned bit, U bitValue) { + auto mask = 1ull << bit; + + return static_cast((value & ~mask) | + ((static_cast(bitValue) << bit) & mask)); +} } // namespace rx