in reply to Making two 8-bit numbers from indices of set bits

use strict; use warnings; my @indicies = qw(8 1 9); my $code; $code |= 1 << $_ for @indicies; printf "0x%02X 0x%02X\n", $code >> 8, $code & 0xFF;

Prints:

0x03 0x02

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Making two 8-bit numbers from indices of set bits
by diotalevi (Canon) on Feb 20, 2007 at 07:25 UTC

    $code |= 1 << $_
    That's what vec is for. It also works for indexes higher than the computer's native integers.

    vec( $code, 1, $_ ) = 1;

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊