Link to home
Start Free TrialLog in
Avatar of IsleOfView
IsleOfView

asked on

Algorithm

I am looking for an algorithm that will allow you to add up squares and determine the individuals.  OK...that doesn't make much sense.  Here's an example.  There are 4 options.  The values to the options are 1, 2, 4, and 8 respectively.  If the total of the options is 14, what routine would let me know that the 2nd, 3rd, and 4th option were picked?
Avatar of ozo
ozo
Flag of United States of America image

$_ = unpack'b*',pack'V',14;
print pos,"\n"  while( /1/g );
Avatar of IsleOfView
IsleOfView

ASKER

That works perfectly.  (I think I need to hit the books to understand the syntax, though) :)

Please choose answer so I can give the points.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#sorry, that second one should have been
for( 1..4 ){ print if 1<<$_-1 & 14 }