A bit pattern can be converted to a list of bit positions set using grep:
bitpattern_2_indices { my $bit_pattern = shift; grep $bit_pattern & 1<<$_, 0..31; } my @indices = bitpattern_2_indices(12345); print "@indices"; # prints 0 3 4 5 12 13
Is there a more efficient way?
Can the range (0..31) be reduced by inspection?
In reply to bit pattern to bit position mapping by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |