wirito has asked for the wisdom of the Perl Monks concerning the following question:
Hello fellow monks,
I'm storing values in a bitwise manner for speed up comparations. I can have 0 or more values in the 0..8 range, so I can store it this way:
When 1 single bit is set I want to retrive the original $val. So currently I have this implemented:$bits |= (1<<$val)
I don't know why, but the while loop seems ugly. Could you enligthen me with a more perlish way to do this? PS: I hope the title is not so much confusing.if( unpack( "%16B*", pack("n", $bits)) == 1 ) { my $ret = 0; while (!($bits & 0x1)) { $ret++; $bits >>= 1; }; return $ret; };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Which bit is set? (inverse of (1<<$value) operator)
by BrowserUk (Patriarch) on Sep 17, 2012 at 11:27 UTC | |
by wirito (Acolyte) on Sep 17, 2012 at 12:11 UTC | |
Re: Which bit is set? (inverse of (1<<$value) operator)
by grizzley (Chaplain) on Sep 17, 2012 at 11:17 UTC | |
by wirito (Acolyte) on Sep 17, 2012 at 12:13 UTC |
Back to
Seekers of Perl Wisdom