busunsl has asked for the wisdom of the Perl Monks concerning the following question:
I have to compile a string of unknown length (more than four bytes) in which several bits have to be set.
I can use
pack 'b*', $bitmap
to create a binary string from a sequence of '0's and '1', e.g. '1100111010'
With this I just have to create my bitmap with something like:
This is very tedious if there are more than a handfull of bits to set.$bitmap = '0' x 64; substr($bitmap, 0, 1) = '1'; substr($bitmap, 0, 4) = '1'; substr($bitmap, 0, 5) = '1'; substr($bitmap, 0, 14) = '1'; $binary = pack('b*', $bitmap);
Next problem is to check if a bit is set in a binary string.
The normal way of ANDing the string won't work because of the size of the string.
I haven't checked Math::BigInt, perhaps that is a simple solution.
Any Ideas?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to set bits in an arbitrary length string
by Masem (Monsignor) on Nov 14, 2001 at 20:11 UTC | |
|
Re: how to set bits in an arbitrary length string
by blakem (Monsignor) on Nov 15, 2001 at 00:13 UTC | |
by busunsl (Vicar) on Nov 15, 2001 at 11:35 UTC | |
|
Re: how to set bits in an arbitrary length string
by I0 (Priest) on Nov 15, 2001 at 01:01 UTC | |
|
Re: how to set bits in an arbitrary length string
by busunsl (Vicar) on Nov 15, 2001 at 12:13 UTC |