Question about BitArrays

Hello,

First time I use bitarray and I have a little question about it :

I have this bitarray :

BitAray = # {1,2,3,4,5,6,7,8,9,10}

I have turn off every bits apart the 1,3, and 5. So at some point in my code I can loop only in the “on” ones and very simply use their items number as a very usefull ID.
My only single problem is, I can’t find how to acess the data itself in the bit array as for instance BitAray[3] will return me “True” and not 3. I can’t find how to do that in the help and I can’t imagine its not possible (after all what would be the point of a bitarray if you can’t use what is inside ??).

Thank you
Bloby

Bitarray values are only every true and false: they are just a cheap way to store lots of boolean values. {1,2,3,4,5,6,7,8,9,10} is really just another way of writing (true, true, true, true, true, true, true, true, true}.

If you want an array of numbers just use a regular array; if you actually need a bit array you’ll need to loop with a counter of your own.

[QUOTE=Theodox;24003]Bitarray values are only every true and false: they are just a cheap way to store lots of boolean values. {1,2,3,4,5,6,7,8,9,10} is really just another way of writing (true, true, true, true, true, true, true, true, true}.

If you want an array of numbers just use a regular array; if you actually need a bit array you’ll need to loop with a counter of your own.[/QUOTE]

I need to “mark” the items of my array just like I do it with a bittArray… And being able to access the data in it… :frowning:

EDIt : Ok I have figure out a workaround… using array and bitarray in parallel… it’s working so…

Thanks ! :slight_smile:

You can very simply loop through bitArray’s indexes like this:


bArr = #{1, 13, 56, 57, 58, 145, 1181, 1188}
for i in bArr do (print i /*or access some dataArray[i] and whatnot*/)

I did a little test just now and in terms of speed it’s roughly the same as looping through a number Array of integer indexes, possibly about 1% slower on small arrays and 1% faster on big arrays. But populating/collect’ing a number array seems 10%-100% slower than setting bits in a bitArray (regardless of pre-initializing array size or not). Add the fact that changing bitArray indexes is much faster than searching a number array for a value and removing it. So perhaps bitArrays are more viable for index-storage use after all.

But yes, you need a regular array in parallel for the “data” part. Which you pretty much figured out.

Ok I see… well thank you very much for this more infos, I see better about the use of bitarrays now. So its pretty much all about optimization (and I found it quite simpler to use for index storage actually).