in reply to Which bit is set? (inverse of (1<<$value) operator)
Use a lookup table:
%lookup = map{ 2**$_ => $_ } 0 .. 63;; $flags = 0;; $flags |= 1<<(rand 64);; print "The flag set is:", $lookup{ $flags };; The flag set is: 12
Some form of loop is necessary to avoid an impossibly large lookup table:
$flags = int rand( 2**64 );; print for grep $flags & 1 << $_, 0 .. 63;; 49 50 51 54 55 57 58 59 printf "%u\n", $flags;; 1066790161733386240 $t = 0; print $t += 1<<$_ for grep $flags & 1 << $_, 0 .. 63;; 562949953421312 1688849860263936 3940649673949184 21955048183431168 57983845202395136 202099033278251008 490329409429962752 1066790161733386240
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Which bit is set? (inverse of (1<<$value) operator)
by wirito (Acolyte) on Sep 17, 2012 at 12:11 UTC |