BitArray返回位周围的错误的方式? [英] BitArray returns bits the wrong way around?

查看:122
本文介绍了BitArray返回位周围的错误的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

BitArray bits = new BitArray(new byte[] { 7 });
foreach (bool bit in bits)
{
    Console.WriteLine(bit ? 1 : 0);
}



给我下面的输出:

Gives me the following output:

11100000

难道不应该是周围的其他方式?像这样的:

Shouldn't it be the other way around? Like this:

00000111

据我所知,很少有和大端,虽然这些术语仅指字节的位置。 。据我所知,他们不影响比特

I am aware that there is little and big endian, although those terms only refer to the position of bytes. As far as I know, they don't affect bits.

推荐答案

documentation为BitArray 的状态:

中的第一个字节数组表示位0到7,第二
字节代表位8至15,等等。每个字节的最低有效
位代表指数最低值:字节[0]放1
代表位0字节[0]和2代表1位,字节[ 0]&放大器; 4
表示第2位,等等

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,所以它们打印出来左到右而不是从右到左。这就是为什么它看起来向后

When indexing bits, the convention is to start at the least significant end, which is the right side when written in binary notation. However, when enumerating the array, you start at index 0, so they are printed out left-to-right instead of right-to-left. That's why it looks backwards.

例如,字01011010 00101101(90 45)将被索引为:

For example, the word 01011010 00101101 (90 45) would be indexed as:

 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 ,这是原始二进制表示法的反向

And you would pass it to the constructor as new byte[] { 45, 90 } since you pass it least-significant first. When printed out, it would display in index order as: 1011010001011010, which is the reverse of the original binary notation.

这篇关于BitArray返回位周围的错误的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆