namespace S7.Net.Types
{
///
/// Contains the methods to read, set and reset bits inside bytes
///
public static class Boolean
{
///
/// Returns the value of a bit in a bit, given the address of the bit
///
public static bool GetValue(byte value, int bit)
{
return (((int)value & (1 << bit)) != 0);
}
///
/// 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)
{
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
///
/// The input value to modify.
/// The index (zero based) of the bit to clear.
public static void ClearBit(ref byte value, int bit)
{
value = (byte) (value & ~(1 << bit) & 0xFF);
}
}
}