From aa5028023371710152fd356bd2053d46747d7df9 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Mon, 10 May 2021 21:14:43 +0200 Subject: [PATCH] Boolean: Add SetBit and ClearBit by reference --- S7.Net/Types/Boolean.cs | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/S7.Net/Types/Boolean.cs b/S7.Net/Types/Boolean.cs index cfb52f8..f7bc83e 100644 --- a/S7.Net/Types/Boolean.cs +++ b/S7.Net/Types/Boolean.cs @@ -14,20 +14,51 @@ } /// - /// Sets the value of a bit to 1 (true), given the address of the bit + /// Sets the value of a bit to 1 (true), given the address of the bit. Returns + /// a copy of the value with the bit set. /// + /// The input value to modify. + /// The index (zero based) of the bit to set. + /// The modified value with the bit at index set. public static byte SetBit(byte value, int bit) { - return (byte)((value | (1 << bit)) & 0xFF); + SetBit(ref value, bit); + + return value; + } + + /// + /// Sets the value of a bit to 1 (true), given the address of the bit. + /// + /// The value to modify. + /// The index (zero based) of the bit to set. + public static void SetBit(ref byte value, int bit) + { + value = (byte) ((value | (1 << bit)) & 0xFF); + } + + /// + /// Resets the value of a bit to 0 (false), given the address of the bit. Returns + /// a copy of the value with the bit cleared. + /// + /// The input value to modify. + /// The index (zero based) of the bit to clear. + /// The modified value with the bit at index cleared. + public static byte ClearBit(byte value, int bit) + { + ClearBit(ref value, bit); + + return value; } /// /// Resets the value of a bit to 0 (false), given the address of the bit /// - public static byte ClearBit(byte value, int bit) + /// The input value to modify. + /// The index (zero based) of the bit to clear. + public static void ClearBit(ref byte value, int bit) { - return (byte)((value & (~(1 << bit))) & 0xFF); + value = (byte) (value & ~(1 << bit) & 0xFF); } - } }