use strict; use warnings; my $bytes = pack 'N', 0x01020304; my $bits = unpack('B32', $bytes); my @counts; while ($bits =~ /(0+|1+)/g) { push @counts, length($1) * (substr($1, 0, 1) == 1 ? 1 : -1); } print "$bits\n@counts";