sub rle { my $rVec = shift; my $bits = length( $$rVec ) *8; my $i = vec( $$rVec, 0, 1 ); my @counts; my $p = 1; while( $p < $bits ) { push @counts, 1; ++$counts[ -1 ] while $p < $bits and vec( $$rVec, $p++, 1 ) == $i; $counts[ -1 ] *= -1 if $i; $i ^= 1; } return \@counts; } ;; $v = pack 'N', 0x01020304;; print unpack 'b*', $v;; 10000000010000001100000000100000 print @{ rle( \$v ) };; -1 8 -1 6 -2 8 -1 5