#!perl -w use strict; use Carp qw(croak); use Data::Dumper; sub get_value { my ($values) = @_; my $result = shift @$values; if (($result & 0b10000000) == 0) { # maybe $result < 127 is faster? return $result } elsif (($result & 0b11000000) == 0b10000000) { # maybe $result < 0b11000000 is faster? return (($result & 0b00111111) << 8) + shift @$values; } elsif (($result & 0b11000000) == 0b11000000) { # Assuming that Perl evaluates even "commutative" expressions # from left to right $result = (($result & 0b00111111) << 16) + ((shift @$values) << 8) + ((shift @$values)); return $result; } else { # This should not happen anyway: croak sprintf "Invalid value found in input: %08b", $result; }; }; my @values = (0b01010101, # 85 0b10101010, 0b10101010, # 10922 0b11001100, 0b11001100, 0b11001100, # 838860 0b00000000, # 0 as another simplicistic testcase ); while (@values) { print "$values[0] => ", get_value( \@values ),"\n"; };