加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:27:02 所属栏目:百科 来源:网络整理
导读:这段代码: BitArray bits = new BitArray(new byte[] { 7 });foreach (bool bit in bits){ Console.WriteLine(bit ? 1 : 0);} 给我以下输出: 11100000 不应该是对方吗?喜欢这个: 00000111 我知道有一点和大端,虽然这些术语只是指字节的位置.据我所知,它
这段代码:
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,它与原始二进制符号相反.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读