设置字节中指定位置Bit位的值
public static byte SetbitValue(byte data, int index, bool flag)
. {
. if (index > 7 || index < 0)
. throw new ArgumentOutOfRangeException();
. int v = 1 << index;
. return flag ? (byte)(data | v) : (byte)(data & ~v);
. }
读取字节中指定位置Bit位的值
public bool GetBit(byte b, int index)
. {
. if (index > 7 || index < 0)
. throw new ArgumentOutOfRangeException();
. bool bit = (b & (1 << index)) != 0;
. return bit;
. }