From aadbd0f9c10f72fba2743018336f368a784489a7 Mon Sep 17 00:00:00 2001 From: DH Date: Thu, 25 Dec 2025 21:58:30 +0300 Subject: [PATCH] rx/bits: add setBits utility --- rx/include/rx/bits.hpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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