www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bits in reverse order in BitArray

reply Christian Manning <cmanning999 gmail.com> writes:
Hi, when I populate a BitArray using .init, the bits are in reverse 
order, although the bytes are in the correct order.
eg:

import std.stdio,std.bitmanip;

void main() {
     ubyte[] arr = [130,56,43,2];

     BitArray ba;
     ba.init(cast(void[])arr,32);
     foreach(b; ba)
        write(cast(ubyte) b); //bits in each byte reversed
                              //01000001000111001101010001000000
     writeln();

     ba.init(cast(void[])arr.reverse,32);
     foreach(b; ba.reverse)
        write(cast(ubyte) b); //bits printed in intended order
                              //10000010001110000010101100000010
     writeln();
}

Why does this happen?

Thanks
Jul 04 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 04 Jul 2011 14:03:35 -0400, Christian Manning  
<cmanning999 gmail.com> wrote:

 Hi, when I populate a BitArray using .init, the bits are in reverse  
 order, although the bytes are in the correct order.
 eg:

 import std.stdio,std.bitmanip;

 void main() {
      ubyte[] arr = [130,56,43,2];

      BitArray ba;
      ba.init(cast(void[])arr,32);
      foreach(b; ba)
         write(cast(ubyte) b); //bits in each byte reversed
                               //01000001000111001101010001000000
      writeln();

      ba.init(cast(void[])arr.reverse,32);
      foreach(b; ba.reverse)
         write(cast(ubyte) b); //bits printed in intended order
                               //10000010001110000010101100000010
      writeln();
 }

 Why does this happen?
What exactly are you expecting? In other words, when using the void[] version of init, you are specifying the exact memory to use, which implies you know how BitArray's internals work. If you create a BitArray by adding bits to the BitArray, then it's internal storage doesn't matter to you, it can store however it wants. I'd guess the most logical reason to store the bits in reverse order would be for performance. Not sure what the merits are, but typically, it's faster to access and manipulate the 0 bit than some arbitrary bit. -Steve
Jul 04 2011
parent Christian Manning <cmanning999 gmail.com> writes:
On 04/07/2011 19:34, Steven Schveighoffer wrote:
 On Mon, 04 Jul 2011 14:03:35 -0400, Christian Manning
 <cmanning999 gmail.com> wrote:

 Hi, when I populate a BitArray using .init, the bits are in reverse
 order, although the bytes are in the correct order.
 eg:

 import std.stdio,std.bitmanip;

 void main() {
 ubyte[] arr = [130,56,43,2];

 BitArray ba;
 ba.init(cast(void[])arr,32);
 foreach(b; ba)
 write(cast(ubyte) b); //bits in each byte reversed
 //01000001000111001101010001000000
 writeln();

 ba.init(cast(void[])arr.reverse,32);
 foreach(b; ba.reverse)
 write(cast(ubyte) b); //bits printed in intended order
 //10000010001110000010101100000010
 writeln();
 }

 Why does this happen?
What exactly are you expecting? In other words, when using the void[] version of init, you are specifying the exact memory to use, which implies you know how BitArray's internals work. If you create a BitArray by adding bits to the BitArray, then it's internal storage doesn't matter to you, it can store however it wants. I'd guess the most logical reason to store the bits in reverse order would be for performance. Not sure what the merits are, but typically, it's faster to access and manipulate the 0 bit than some arbitrary bit. -Steve
Sorry, I assumed it was BitArray causing this, but that's just what made me aware of it. It seems dmd does this when accessing pointers directly, so your reasoning is probably right! Thanks!
Jul 04 2011