in reply to Handling bits in Perl

You say you have a data stream coming from a device. This which may mean there is some structure to the data stream. unpack() is a great tool and will go down to the single bit level with "B" and "b" specs. It can be used along with the tools already mentioned to do pretty much anything you want.

Remember that you can use successive unpack()s to pick things apart and leave some, as in

use strict; my $str = '567'; my ($ch, $rest); ($ch, $rest) = unpack('a a*', $str); while (length $ch) { my $high_nib= ord($ch)>>4; my $low_nib = ord($ch) & 15; my ($high_bit,$case,$alpha,$rangel,$bit3,$bit2,$bit1,$bit0) = split '', unpack('B*',$ch); my @bits = split '', unpack('B*',$ch); print "$ch == @bits ... "; printf "%d, %d, %d, %d : %d/%d\n", $high_bit,$case,$alpha,$rangel,$high_nib,$low_nib; ($ch, $rest) = unpack('a a*', $rest); } # prints # 5 == 0 0 1 1 0 1 0 1 ... 0, 0, 1, 1 : 3/5 # 6 == 0 0 1 1 0 1 1 0 ... 0, 0, 1, 1 : 3/6 # 7 == 0 0 1 1 0 1 1 1 ... 0, 0, 1, 1 : 3/7

Replies are listed 'Best First'.
Re^2: Handling bits in Perl
by Cuhulain (Beadle) on Aug 05, 2006 at 18:14 UTC
    Most Gracious Monks,

    I read a bitstream from a Socket, running as a socket server. Data seems to arrive in streams of several kilobits, before the remote client closes the socket. (My study of http://www.perlmonks.com/index.pl?node_id=21054 suggests I should replace <> with sysread, in the hope of a more real-time read, but that is on the different topic of buffering and blocking.)

    I am using Bit::Vector on these long bitstreams. But on re-reading the manual, I see some Bit::Vector operations are limited to 32 bits. (Block_Read, for example.) I'd appreciate some words of enlightenment on the safe way to handle very long bitstreams.

    Should I laboriously process the bitstream 8-bits at a time, perhaps. Maybe using whatever is Bit::Vector's equivalent of rodion's previous posting on unpack( 'a a*', $rest )?

    Many fields in the definiton of the bitstream are more than 8 bits long, some over 16. The spectre on endian-ness is haunting me.

    Thank-you.