2

If my class uses the BitArray and BitConverter classes, will the endianess of both always match?

The documentation for the BitConverter constructor explicitly mentions that it depends on the architecture it is being used on (so nearly always little-endian), however the BitArray(Byte) constructor documentation just says:

The first byte in the array represents bits 0 through 7, the second byte represents bits 8 through 15, and so on

But this does not specify if the endianess scheme holds for using BitArray.CopyTo(), for example.

EDIT

An example where this might matter is:

Dim MyBitArray As BitArray = New BitArray(16, false)
'...here some values might have been assigned to the bit array...
Dim ba As Byte() = New Byte() {0, 0}
MyBitArray.CopyTo(ba, 0) 'Copy bit array to byte array
Dim x As Int16 = BitConverter.ToInt16(ba, 0)'Converter expects little endian, but did CopyTo put the bytes into the array in the right order?
Toby
  • 9,696
  • 16
  • 68
  • 132
  • Bits do not have an endian problem, everybody agrees that bit 0 is the least significant one. – Hans Passant Feb 11 '16 at 16:50
  • @HansPassant This is true. But if I use the code `MyBitArray.CopyTo(MyByteArray, 0)` will the first byte in MyByteArray be always most or least significant? – Toby Feb 11 '16 at 16:53
  • @HansPassant Actually it seems it's *not* true. See Jon Skeet's answer at https://bytes.com/topic/c-sharp/answers/246789-bitarray-little-endian-bit-order-bytes – Toby Feb 11 '16 at 16:55
  • It is an array, it is up to you to decide what element 0 represents. It only starts to matter when you create a value of another type from the array. For which you'd use BitConverter. Endianness is a totally moot issue today anyway, XBox 360 was the last platform that was big. Avoid fretting over non-existing problems. – Hans Passant Feb 11 '16 at 17:00
  • @HansPassant I've added an edit of an example where this might matter – Toby Feb 11 '16 at 17:06
  • Also, sure most architectures that run windows are little-endian these days. But a lot of communications protocols are big-endian, as are the systems that they might be connected to. – Toby Feb 12 '16 at 12:16

0 Answers0