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?
IsleOfViewAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
$_ = unpack'b*',pack'V',14;
print pos,"\n"  while( /1/g );
#or
for( 1..4 ){ print if $_&14 }
0
 
ozoCommented:
$_ = unpack'b*',pack'V',14;
print pos,"\n"  while( /1/g );
0
 
IsleOfViewAuthor Commented:
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.
0
 
ozoCommented:
#sorry, that second one should have been
for( 1..4 ){ print if 1<<$_-1 & 14 }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.