1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/opt/perl use strict; use warnings; use feature qw(say); my @values = (1, 0, 1, 1); my $value = 0; for (my ($idx, $pw) = ($#values, 0); $idx >= 0; --$idx, ++$pw) { # warn(sprintf("idx: $idx: [idx]: $values[$idx]: %s", 2 ** $idx)); $value += $values[$idx] * (2 ** $pw); } say($value);
You can clone a snippet to your computer for local editing. Learn more.