BitArray - Is there one?

Era Scarecrow rtcvb32 at yahoo.com
Mon May 28 17:13:26 PDT 2012


On Monday, 28 May 2012 at 21:59:36 UTC, Dmitry Olshansky wrote:
>
> Check your math. Xor != sub.
>  1 ^ 0 == 1, 0 ^ 1 == 1;
> Compare with
>  1 <sub> 0 == 1, 0 <sub> 1 == 0.
>
> That is, for one thing, sub is asymmetric :)

  Hmm well subs would all happen at the 1 bit level. So let's 
compare.
xor
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

sub
0 - 0 = 0
0 - 1 = -1 (or non-zero/true, truncates to 1)
1 - 0 = 1
1 - 1 = 0

  Sorry, seems the same unless we are going with carry rules, (at 
which point it is no longer bit arrays subs), it seems to follow 
the xor rules.

> I think it would be nice to rewrite operators to new style. Cut 
> down of the sheer repetition is good enough reason to merge it.
>
> Alas the other primary thing it lacks aside from set operators 
> overloading is Small string optimization. Namely you have some 
> 8bytes here, that's more then enough to store say 7*8=56 bits, 
> and have 7 bits for length. Presumably you check if it's a 
> small set by using lower bit of a pointer. e.g. 0 == normal 
> pointer, big set. if set to 1 == small set, everything else in 
> next 3 bits of "pointer" are small-length, all other 7 bytes of 
> the whole struct contain bit-array. You still can operate on it 
> as 2 words.

  Union work. Could do it, but it then requires special rules. To 
do slices I have 2 longs representing the offset and ending bits 
within the size_t array, so we're looking at 192-256 bits. minus 
16 will still leave us with 128 workable bits.

  Dealing with the pointer seems like a bad idea, but my starting 
offset we can say the first two bytes are 255, dropping the 
usable range of the starting offset to about 2^64 - 2^32, That 
could work and keep almost all the functionality.

> Then it can have very interesting use cases. Like say 
> completely replace usual nitty-gritty C-ish bit-flags. They 
> sadly are too cost effective so that no suitable safe 
> alternative came about yet. Time to make a game changer? ;)

  I don't see BitArray as a good replacement for the C flags. 
However I already made (and suggested) a flags struct that 
accepts and uses enums for it's flags.

> (and of course this optimization would be very welcome, I'll 
> happily bash any walls in front of such contribution making 
> into Phobos)

  Then I'll import it and Hope I don't get too much criticism from 
the community.


More information about the Digitalmars-d-learn mailing list