473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

algorithm question - selecting bits

131 128KB
I've got defines representing option choices where the result can be none, all, or any combination. I'm using an if / else if / else structure to test which options are set, but I wondering if there is a more logical / efficient way of doing this?

So far, I've got this

Expand|Select|Wrap|Line Numbers
  1. #define OPT_NONE  0
  2. #define OPT_1     1
  3. #define OPT_2     2
  4.       ...
  5. #define OPT_7    64
  6. #define OPT_8   128
  7. #define OPT_ALL 255
  8.  
  9. unsigned int result = OPT_NONE;
  10.  
The user can then select which options he or she wants, and I build a query string based the selection(s).

Expand|Select|Wrap|Line Numbers
  1. switch(user_entered_choice)
  2. {
  3.    case OPT_NONE : result = OPT_NONE; break;
  4.    case OPT_1    : result += OPT_1;   break;
  5.        ...
  6.    case OPT_8    : result += OPT_8;   break;
  7. }
  8.  
  9. if(result == OPT_NONE)
  10.    // default option selected
  11. else if(result == OPT_ALL)
  12.    // all options selected
  13. else if(result == OPT_1 ... result == OPT_8) // not valid code
  14.    // a single option 1 of 8 was selected
  15. else if(((result & OPT_1) == OPT_1) &&
  16.         ((result & OPT_2) == OPT_2) ... 
  17.  
The last else if bit of code seems way too clumsy, but I'm at a loss trying to figure out how to combine these selections. Would I even want to use this approach?

TIA
Apr 17 '14 #1

✓ answered by weaknessforcats

I wouldn't worry about efficiency until the program works.

One time I wrote:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      while(1)
  4.      {
  5.         featureAdd(choice);
  6.         featureRevise(choice);
  7.         featureDelete(choice);
  8.         etc...
  9.      }
  10. }
where each function:

Expand|Select|Wrap|Line Numbers
  1. void featureAdd()
  2. {
  3.     if (choice != ADD)
  4.     return;
  5.  
  6.     etc...to do the add
  7. }
because no one could tell me what the menu was or when the add feature may be called. As a result my feature functions inside the loop can be in any order without changing the logic of the program.

Efficient? not really.

Practical? an absolute lifesaver.

2 1459
weaknessforcats
9,208 Expert Mod 8TB
I wouldn't worry about efficiency until the program works.

One time I wrote:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      while(1)
  4.      {
  5.         featureAdd(choice);
  6.         featureRevise(choice);
  7.         featureDelete(choice);
  8.         etc...
  9.      }
  10. }
where each function:

Expand|Select|Wrap|Line Numbers
  1. void featureAdd()
  2. {
  3.     if (choice != ADD)
  4.     return;
  5.  
  6.     etc...to do the add
  7. }
because no one could tell me what the menu was or when the add feature may be called. As a result my feature functions inside the loop can be in any order without changing the logic of the program.

Efficient? not really.

Practical? an absolute lifesaver.
Apr 17 '14 #2
divideby0
131 128KB
Thank you for the reply. I *think* that I've got it worked out... well it seems to return what I expect.

If there's a silver lining, it's that I don't have a time constraint to worry about or someone to answer to, so I can putz around with it when I feel up to the madness. :)

I cannot thank you, Banfa, and Rabbit enough for all of the help that each of you has given.

Cheers.
Apr 19 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
4
by: Kinsley Turner | last post by:
Hey-ho, I'm getting a bit out of my depth porting the 'tiny encryption algorithm' from C to python. Ref: http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
8
by: Hahnemann | last post by:
Is there a sample in C out there to represent a sequence of numbers (shorts) using bits? With the intention of saving space, instead of using the full 16 bits per short. Thanks. - Hahnemann
9
Rabbit
by: Rabbit | last post by:
Introduction The Advanced Encryption Standard is the algorithm that won the National Insitute of Standards and Technology's (NIST) search for a standardized encryption algorithm in 2001. In 2002, it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.