From 9422a09e6b62ee99b288a3b58bdfada37dc532c1 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 8 Sep 2024 12:12:26 +0100 Subject: [PATCH] Codechange: Add AssignBit function to assign the value of a single bit --- src/core/bitmath_func.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index ffda4b2eeb..f627b2e308 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -183,6 +183,25 @@ constexpr T ToggleBit(T &x, const uint8_t y) return x = (T)(x ^ ((T)1U << y)); } +/** + * Assigns a bit in a variable. + * + * This function assigns a single bit in a variable. The variable is + * changed and the value is also returned. Parameter y defines the bit + * to assign and starts at the LSB with 0. + * + * @param x The variable to assign the bit + * @param y The bit position to assign + * @param value The new bit value + * @pre y < sizeof(T) * 8 + * @return The new value of the old value with the bit assigned + */ +template +constexpr T AssignBit(T &x, const uint8_t y, bool value) +{ + return SB(x, y, 1, value ? 1 : 0); +} + /** * Search the first set bit in a value. * When no bit is set, it returns 0.