namespace PLCSharp.Core.Tools
{
///
/// Bit工具
///
public static class BitTool
{
#region uint
///
/// ToBitArray
///
/// int值
/// 返回结果
public static bool[] ToBitArray(this uint intValue)
{
bool[] bitArray = new bool[32];
for (var i = 0; i <= 31; i++)
{
var val = 1 << i;
bitArray[i] = (intValue & val) == val;
}
return bitArray;
}
///
/// 获取Bit
///
/// int值
/// 当前索引
/// 返回布尔值
public static bool GetBit(this uint intValue, int index)
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
return (intValue & val) == val;
}
///
/// 设置Bit
///
/// int值
/// 当前索引
public static void SetBit(ref this uint intValue, int index)
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
intValue |= (uint)val;
}
///
/// 重置Bit
///
/// int值
/// 当前索引
public static void ResetBit(ref this uint intValue, int index)
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
intValue &= ~(uint)val;
}
#endregion
#region int
///
/// ToBitArray
///
/// int值
/// 返回结果
public static bool[] ToBitArray(this int intValue)
{
bool[] bitArray = new bool[32];
for (var i = 0; i <= 31; i++)
{
var val = 1 << i;
bitArray[i] = (intValue & val) == val;
}
return bitArray;
}
///
/// 获取Bit
///
/// int值
/// 当前索引
/// 返回布尔值
public static bool GetBit(this int intValue, int index)
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
return (intValue & val) == val;
}
///
/// 设置Bit
///
/// int值
/// 当前索引
public static void SetBit(ref this int intValue, int index)
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
intValue |= val;
}
///
/// 重置Bit
///
/// int值
/// 当前索引
public static void ResetBit(ref this int intValue, int index)
{
if (index > 31 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
intValue &= ~val;
}
#endregion
#region byte
///
/// ToBitArray
///
/// byte值
/// 返回结果
public static bool[] ToBitArray(this byte byteValue)
{
bool[] bitArray = new bool[8];
for (var i = 0; i < 8; i++)
{
var val = 1 << i;
bitArray[i] = (byteValue & val) == val;
}
return bitArray;
}
///
/// Bit
///
/// byte值
/// 当前索引
/// tf
public static void Bit(ref this byte byteValue, int index, bool tf)
{
if (index >= 8 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
if (tf) byteValue |= (byte)val; else byteValue &= (byte)~val;
}
///
/// 获取Bit
///
/// byte值
/// 当前索引
/// 返回布尔值
public static bool GetBit(this byte byteValue, int index)
{
if (index >= 8 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
return (byteValue & val) == val;
}
///
/// 设置Bit
///
/// byte值
/// 当前索引
public static void SetBit(ref this byte byteValue, int index)
{
if (index >= 8 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
byteValue |= (byte)val;
}
///
/// ResteBit
///
/// byte值
/// 当前索引
public static void ResteBit(ref this byte byteValue, int index)
{
if (index >= 8 || index < 0) throw new Exception("索引越界");
var val = 1 << index;
byteValue &= (byte)~val;
}
#endregion
}
}