473,386 Members | 1,721 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,386 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 1461
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.