Skip to content

Commit

Permalink
update params descriptions and change param name from "bitPosition" t…
Browse files Browse the repository at this point in the history
…o "index"
  • Loading branch information
Himmelt committed Aug 17, 2023
1 parent 652ff3a commit fadd7d0
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions S7.Net/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,40 +138,41 @@ public static string ValToBinString(this object value)

/// <summary>
/// Helper to get a bit value given a byte and the bit index.
/// Example: DB1.DBX0.5 -> var bytes = ReadBytes(DB1.DBW0); bool bit = bytes[0].SelectBit(5);
/// Example: DB1.DBX0.5 -> var bytes = ReadBytes(DB1.DBW0); bool bit = bytes[0].SelectBit(5);
/// </summary>
/// <param name="data"></param>
/// <param name="bitPosition"></param>
/// <returns></returns>
public static bool SelectBit(this byte data, int bitPosition)
/// <param name="data">byte data get from</param>
/// <param name="index">bit index</param>
/// <returns>bool value will get</returns>
public static bool SelectBit(this byte data, int index)
{
int mask = 1 << bitPosition;
int mask = 1 << index;
int result = data & mask;

return (result != 0);
}

/// <summary>
/// Helper to set a bit value to the given byte at the bit index.
/// Example: byte data = (byte)0x00; data.SetBit(4, true);// data -> 0x10
/// </summary>
/// <param name="data"></param>
/// <param name="bitPosition"></param>
/// <param name="value"></param>
public static void SetBit(this ref byte data, int bitPosition, bool value)
/// <param name="data">byte data to be modified</param>
/// <param name="index">bit index</param>
/// <param name="value">bool value to set</param>
public static void SetBit(this ref byte data, int index, bool value)
{
if ((uint)bitPosition > 7)
if ((uint)index > 7)
{
return;
}

if (value)
{
byte mask = (byte)(1 << bitPosition);
byte mask = (byte)(1 << index);
data |= mask;
}
else
{
byte mask = (byte)~(1 << bitPosition);
byte mask = (byte)~(1 << index);
data &= mask;
}
}
Expand Down

0 comments on commit fadd7d0

Please sign in to comment.