16

This code:

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.

tshepang
12.5k25 gold badges98 silver badges140 bronze badges
asked Jan 30, 2012 at 16:04

3 Answers 3

13

The documentation for BitArray states:

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.

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.

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

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.

answered Jan 30, 2012 at 16:10
Sign up to request clarification or add additional context in comments.

Comments

3

No it's a bit array, not a numeric value represented as bits.

It's just like any regular array with some methods added for bit operations. Just like if you had an array of int. You wouldn't expect it to be in the reverse order, it just takes it position by position.

Eg:

Numbers (in a byte) converted to a BitArray would come out like:

2 = 01000000
5 = 10100000
8 = 00010000

etc.

It just stores the position of the value but not relative as you would except from a binary numeric value.

Here is a link describing the constructor you are using:

http://msdn.microsoft.com/en-us/library/b3d1dwck.aspx

The key point is:

The number in the first values array element represents bits 0 through 31, the second number in the array represents bits 32 through 63, and so on. The Least Significant Bit of each integer represents the lowest index value: " values [0] & 1" represents bit 0, " values [0] & 2" represents bit 1, " values [0] & 4" represents bit 2, and so on.

answered Jan 30, 2012 at 16:10

Comments

2

The documentation is not explicit about it, but I guess the iterator iterates from the LSB to the MSB. Sound reasonable to me (personally!), unless you are printing out the bits. I had a look at BitArray.GetEnumerator Method.

answered Jan 30, 2012 at 16:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.