BitArray shift left/right confusion.

Bastiaan Veelo Bastiaan at Veelo.net
Wed Dec 27 18:08:19 UTC 2017


I suppose the following is not a bug, but confusing it is:

```
void main()
{
     import std.stdio;
     import std.bitmanip;
     BitArray ba = [1, 1, 1, 1, 1, 1, 1, 1];
     writeln(ba);	// [1, 1, 1, 1, 1, 1, 1, 1]
     ba >>= 4;		// right shift
     writeln(ba);	// [1, 1, 1, 1, 0, 0, 0, 0] bits shifted left
}```

I suppose this is because the array is printed left-to-right, 
whereas the bits in a byte are typically ordered right-to-left. I 
suppose I should interpret the bits in the array to increase in 
significance with increasing index (little endian) and that 
right-shift means a shift towards less significance (which is to 
the right in big endian).

The documentation of <<= and >>= [1] however just talks about 
left and right, without defining left and right or clarifying 
that the directions are reversed from how the array is printed.

Is there something I have missed?

[1] 
https://dlang.org/phobos/std_bitmanip.html#.BitArray.opOpAssign.2


More information about the Digitalmars-d-learn mailing list