using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrpaonEMS.App.Com
{
public class BitHelper
{
///
/// 获取字的高字节
///
public static byte GetHByteUint16(ushort data)
{
return (byte)(data >> 8);//高位
}
///
/// 获取字的低字节
///
public static byte GetLByteUint16(ushort data)
{
return (byte)(data & 0xff);//低位
}
///
/// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组
/// 从右侧开始向左索引(0~31)
///
///
///
public static IEnumerable GetBitList(int value)
{
var list = new List(32);
for (var i = 0; i <= 31; i++)
{
var val = 1 << i;
list.Add((value & val) == val);
}
return list;
}
///
/// 返回Int数据中某一位是否为1
///
///
/// 32位数据的从右向左的偏移位索引(0~31)
/// true表示该位为1,false表示该位为0
public static bool GetBitValue(int value, ushort index)
{
if (index > 31) throw new ArgumentOutOfRangeException("index"); //索引出错
var val = 1 << index;
return (value & val) == val;
}
///
/// 设定Int数据中某一位的值
///
/// 位设定前的值
/// 32位数据的从右向左的偏移位索引(0~31)
/// true设该位为1,false设为0
/// 返回位设定后的值
public static int SetBitValue(int value, ushort index, bool bitValue)
{
if (index > 31) throw new ArgumentOutOfRangeException("index"); //索引出错
var val = 1 << index;
return bitValue ? (value | val) : (value & ~val);
}
}
}