c# – BitArray返回位错误的方式?

前端之家收集整理的这篇文章主要介绍了c# – BitArray返回位错误的方式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这段代码
BitArray bits = new BitArray(new byte[] { 7 });
foreach (bool bit in bits)
{
    Console.WriteLine(bit ? 1 : 0);
}

给我以下输出

11100000

不应该是对方吗?喜欢这个:

00000111

我知道有一点和大端,虽然这些术语只是指字节的位置.据我所知,它们不影响位.

解决方法

documentation for BitArray说:

The first byte in the array represents bits 0 through 7,the second
byte represents bits 8 through 15,and so on. The Least Significant
Bit of each byte represents the lowest index value: ” bytes [0] & 1″
represents bit 0,” bytes [0] & 2″ represents bit 1,” bytes [0] & 4″
represents bit 2,and so on.

当索引位时,约定是从最低有效端开始,右边是用二进制表示法写入的.但是,当枚举数组时,从索引0开始,因此它们从左到右打印,而不是从右到左打印.这就是为什么它看起来倒退.

例如,字01010101010101101(9045)将被索引为:

0  1  0  1  1  0  1  0  -  0  0  1  0  1  1  0  1
-----------------------    -----------------------
15 14 13 12 11 10  9  8     7  6  5  4  3  2  1  0

并且你会把它传递给构造函数作为新的byte [] {45,90},因为你传递最不重要的.当打印输出时,它将以索引顺序显示为:1011010001011010,它与原始二进制符号相反.

原文链接:https://www.f2er.com/csharp/96195.html

猜你在找的C#相关文章