-2
A
回答
3
您可以使用BitArray()构造BitArray((Byte[])摆脱字节数组位阵列。在位数组上使用索引器来获取所需的位。
var bits = new BitArray(_byte);
现在位16将是位[15];
你可以按照这个例子来理解你如何获得需求位。
byte[] _byte = new byte[3] {1,3,7};
var bits = new BitArray(_byte);
for(int i=0; i < bits.Length; i++)
Console.WriteLine(bits[i]);
输出:
真本1在比特和假出现0在比特
第一字节值= 1 = 00000001(二进制)
True = 1 0 bit index
False = 0 1 bit index
False = 0 2 bit index
False = 0 3 bit index
False = 0 4 bit index
False = 0 5 bit index
False = 0 6 bit index
False = 0 7 bit index
第二个字节 Value = 3 = 00000011(Binary)
True = 1 8 bit index
True = 1 9 bit index
False = 0 10 bit index
False = 0 11 bit index
False = 0 12 bit index
False = 0 13 bit index
False = 0 14 bit index
False = 0 15 bit index
第三个字节值= 7 = 00000111(二进制)
True = 1 16 bit index
True = 1 17 bit index
True = 1 18 bit index
False = 0 19 bit index
False = 0 20 bit index
False = 0 21 bit index
False = 0 22 bit index
False = 0 23 bit index
1
如果你是想用位工作,你可以使用BitArray类。它的一个构造函数重载允许你传入一个字节数组。另外需要注意的是,该位将由一个布尔值表示,其真值为'1'并且为假'0'。
1
如果你想在你的字节数组的X位(我认为这是你的要求至少),你从阵列需要索引正确的字节,然后提取位
public static Boolean GetBitX(byte[] bytes, int x) {
var index = x/8;
var bit = x-index*8;
return (bytes[index] & (1<<bit)) != 0;
}
1
public static Boolean GetBitX(byte[] bytes, int x) {
var index = x/8;
System.Collections.BitArray ba = new BitArray(new byte[]{bytes[index]});
return ba.Get(x);
}
您可以使用BitArray
相关问题
- 1. 从字节获取位图[]
- 2. 从字节串获取任意位bits
- 3. 获取从位图的字节数组
- 4. gosnmp从SNMP PDU获取八位字节
- 5. shell - 如何从一个字节中单独获取每一位?
- 6. 如何从十六进制值中提取低位字节?
- 7. 从NSData的获取浮点值字节
- 8. 如何从UnsafeMutableRawPointer中获取字节?
- 9. 如何从PL/SQL中的raw获取特定的字节值?
- 10. 从一个字节获取半字节
- 11. 如何从字节数组([UInt8])获取一个字节(UInt8)?
- 12. 从XML获取节点值
- 13. 按位运算符从32位获取字节
- 14. 从IBUFFER获取字节
- 15. 从UIImage获取字节
- 16. 从位置获取值
- 17. 如何从字符串中获取值?
- 18. 如何从masterRow获取字段值
- 19. 如何从json字符串获取值?
- 20. 字节到BMP中获取RGB的位
- 21. Javascript。从特定位置获取节点
- 22. 如何获取PHP中字符串的字节值?
- 23. 获取HTTPRequest/HTTPResponse头字节大小(以字节为单位)
- 24. 如何从字节返回位
- 25. 如何获取从QTextStream中读取的字节数
- 26. PIC C18:从一个字节读取位
- 27. 从字节中抓取n位
- 28. 从字节数组读取3位
- 29. 从字符串获取字节
- 30. 如何从原始Bayer图像数据(字节数组)获取位图图像?
您不能得到第17位,_byte只有8位... – Radagast
它有64位...它是8字节的字节.. 8 * 8 = 64知道吗? –
@ArnaldoBadin错误 – MickyD