in reply to Re: Displaying bit-vectors with Most Significant Bits to the left?
in thread Displaying bit-vectors with Most Significant Bits to the left?
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Displaying bit-vectors with Most Significant Bits to the left?
by LanX (Saint) on Jan 05, 2020 at 15:18 UTC | |
by ikegami (Patriarch) on Jan 06, 2020 at 07:09 UTC | |
by LanX (Saint) on Jan 06, 2020 at 14:33 UTC | |
by ikegami (Patriarch) on Jan 07, 2020 at 18:47 UTC |