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"; #### 00000001000000100000001100000100 -7 1 -6 1 -7 2 -5 1 -2