The OP didn't give any indication that $m0 was different than expected. Changing the value of $m0 doesn't seem appropriate. But if it is, there are two improvements you can make.
When doing bit arithmetic, most people prefer to use actual bit arithmetic instead of powers. Not only is it more intuitive to use operations that match one's mental model, your approach fails if a bit is already set. Replace
$val += 2 ** $_
with
$val |= 1 << $_
As evidenced by these posts, vec is quite weird, so it's surely best to avoid it if it doesn't offer a benefit.
my $vec;
vec( $vec, 0, 16 ) = $val;
can be replaced with
my $vec = pack 'n', $val;
|