mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
Boolean: Add SetBit and ClearBit by reference
This commit is contained in:
@@ -14,20 +14,51 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="value">The input value to modify.</param>
|
||||
/// <param name="bit">The index (zero based) of the bit to set.</param>
|
||||
/// <returns>The modified value with the bit at index set.</returns>
|
||||
public static byte SetBit(byte value, int bit)
|
||||
{
|
||||
return (byte)((value | (1 << bit)) & 0xFF);
|
||||
SetBit(ref value, bit);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of a bit to 1 (true), given the address of the bit.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to modify.</param>
|
||||
/// <param name="bit">The index (zero based) of the bit to set.</param>
|
||||
public static void SetBit(ref byte value, int bit)
|
||||
{
|
||||
value = (byte) ((value | (1 << bit)) & 0xFF);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="value">The input value to modify.</param>
|
||||
/// <param name="bit">The index (zero based) of the bit to clear.</param>
|
||||
/// <returns>The modified value with the bit at index cleared.</returns>
|
||||
public static byte ClearBit(byte value, int bit)
|
||||
{
|
||||
ClearBit(ref value, bit);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the value of a bit to 0 (false), given the address of the bit
|
||||
/// </summary>
|
||||
public static byte ClearBit(byte value, int bit)
|
||||
/// <param name="value">The input value to modify.</param>
|
||||
/// <param name="bit">The index (zero based) of the bit to clear.</param>
|
||||
public static void ClearBit(ref byte value, int bit)
|
||||
{
|
||||
return (byte)((value & (~(1 << bit))) & 0xFF);
|
||||
value = (byte) (value & ~(1 << bit) & 0xFF);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user