http://www.perlmonks.org?node_id=994008

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:
$bits |= (1<<$val)
When 1 single bit is set I want to retrive the original $val. So currently I have this implemented:
if( unpack( "%16B*", pack("n", $bits)) == 1 ) { my $ret = 0; while (!($bits & 0x1)) { $ret++; $bits >>= 1; }; return $ret; };
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.