BitArray contracts

Steven Schveighoffer schveiguy at yahoo.com
Tue Aug 24 06:19:22 PDT 2010


On Mon, 23 Aug 2010 22:49:14 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> This a part of std.bitmanip.BitArray:
>
>
>     void init(void[] v, size_t numbits)
>     in
>     {
>         assert(numbits <= v.length * 8);
>         assert((v.length & 3) == 0);
>     }
>     body
>     {
>         ptr = cast(uint*)v.ptr;
>         len = numbits;
>     }
>
>
> But it seems this program works with no errors:
>
> import std.bitmanip: BitArray;
> void main() {
>     ubyte[4] data;
>     BitArray bits;
>     bits.init(data, 100);
> }

If bitarray is not a template, then it's compiled in release mode inside  
phobos.  The in contract is probably not compiled in.

> Do you kno why is this assert present?
> assert((v.length & 3) == 0);
>
> Isn't this enough?
> assert((v.length & 2) == 0);

The first is asserting that v.length is a multiple of 4, I think the point  
is to be able to manipulate all the data using words.  Yours is asserting,  
well, it's asserting that the second bit is not set.  That yields the  
following sequence:

0,1,4,5,8,9,12,13...

I'm not sure why you want that sequence.

-Steve


More information about the Digitalmars-d-learn mailing list