>>>>> "TM" == Tom Metro <[EMAIL PROTECTED]> writes:
TM> Stephen A. Jarjoura wrote:
>> ...what if I had needed to store/retrieve some arbitrary number of bits?
TM> Uri's suggestion of Bit::Vector is probably what you want, but more
TM> generally, take a look at the perlop man page and read up on the
TM> bitwise operators such as &, |, ^, and the shift operators <<, >>.
beware those operators for one major reason. they behave differently
based on how their data was last used. if the data was used in integer
math, then the bit ops are integer ops and exactly 32 bits (or whatever
your normal integer size is). if the data was last handled as a string,
then the ops are string type and will work on from 0 to N bytes of
data. this was a very poor decision by larry to overload operators based
upon the past history of the data. perl6 fixes this with separate math
and string bit ops.
TM> # Retrieve four fields (2 bits, 6 bits, 3 bits, 5 bits) from 2 bytes
TM> sub custom_unpack {
TM> my $bits = shift;
TM> my @unpacked;
TM> push(@unpacked, ($bits & 0xC000) >> 14);
TM> push(@unpacked, ($bits & 0x3F00) >> 8);
TM> push(@unpacked, ($bits & 0x00E0) >> 5);
TM> push(@unpacked, $bits & 0x001F);
this is one place where c excells over perl! :)
and of course it is tricky to handle longer bit strings and fields
without bugs. that is why bit::vector is the best answer for this.
uri
--
Uri Guttman ------ [EMAIL PROTECTED] -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm