Solved

Algorithm

Posted on 1998-09-04
4
189 Views
Last Modified: 2010-03-05
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?
0
Comment
Question by:IsleOfView
  • 3
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204693
$_ = unpack'b*',pack'V',14;
print pos,"\n"  while( /1/g );
0
 

Author Comment

by:IsleOfView
ID: 1204694
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
 
LVL 84

Accepted Solution

by:
ozo earned 10 total points
ID: 1204695
$_ = unpack'b*',pack'V',14;
print pos,"\n"  while( /1/g );
#or
for( 1..4 ){ print if $_&14 }
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204696
#sorry, that second one should have been
for( 1..4 ){ print if 1<<$_-1 & 14 }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now