XP is just a number | |
PerlMonks |
Re: Re: Re: Re: fast bit twiddlingby BrowserUk (Patriarch) |
on May 03, 2004 at 06:20 UTC ( [id://349934]=note: print w/replies, xml ) | Need Help?? |
Agreed. You can pack the bits to form a number and then manipulate them using boolean operators. I found two problems with that approach though. The first is converting a list of bit-indices to a (set of) masks to allow the bit manipulations is pretty inefficient. Zaxo neatly sidestepped that by taking the list (2, 5, 8) as being a fixed list and performing the conversion once. If that is the case in the OP's requirements than his method (or yours:) will probably be much quicker, but that wasn't my interpretation of the OP. The second problem is that whilst the bit-string given as an example only had 10-bits, it was far from clear that this was the limit, or even typical. If the length of the bit-string moves beyond the largest native-integer size, then the boolean manipulations fall down. Only the OP can know exactly what form his data takes and therefore which approach is best for his application. If he can obtain/maintain his bits as bits rather than bytes, that would definitely be the way to go. Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"Think for yourself!" - Abigail
In Section
Seekers of Perl Wisdom
|
|