C#获取比特位
作者:互联网
///
/// 获取字节中的指定Bit的值
///
/// 字节
/// Bit的索引值(0-7)
///
public int GetBit(byte test, int index)
{
byte x = 1;
switch (index)
{
case 0: { x = 0x01; } break;
case 1: { x = 0x02; } break;
case 2: { x = 0x04; } break;
case 3: { x = 0x08; } break;
case 4: { x = 0x10; } break;
case 5: { x = 0x20; } break;
case 6: { x = 0x40; } break;
case 7: { x = 0x80; } break;
default: { return 0; }
}
return (test & x) == x ? 1 : 0;
}
public void Test()
{
byte[] results = new byte[1];
results[0] = 0xbb;
int temp = results[0];
for (int i=7;i>=0;i--)
{
var itemtest=GetBit(results[0],i);
Console.WriteLine(itemtest);
}
}
标签:case,比特,C#,results,break,获取,int,byte,GetBit 来源: https://www.cnblogs.com/LCLBook/p/16494959.html