Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4238814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:57:11+00:00 2026-05-21T02:57:11+00:00

I am new to C, and am having some fun playing around with bitwise

  • 0

I am new to C, and am having some fun playing around with bitwise operations. What I am doing seems to work, I am just wondering if it is considered good style.

Alright so let’s say my program has 3 command-line options, -a, -b, and -c. Previously I would have had 3 ints act as booleans, say aFlag, bFlag, and cFlag. Then when I call my processOpts( int *aFlag, int *bFlag, int *cFlag) function, I would pass &aFlag, &bFlag, and &cFlag as arguments, and set them like *aFlag = 1.

Here’s my new approach: have 1 int *options to represent all of the options, and treat it like an array of booleans. So to set them in the function:

case 'a':
  *options |= 1;
  break;
case 'b':
  *options |= 2;
  break;
case 'c':
  *options |= 4;
  break;

Then, back in main (or wherever), when I want to test to see if an option is chosen:

if ( *options & 1 )
  // Option 'a' selected

if ( *options & 2 )
  // Option 'b' selected

if ( *options & 4 )
  // Option 'c' selected

My question is: which method is considered better style? The first way could be more clear and less error-prone, whereas the second would probably make for easier refactoring (no need to change function prototype, as it’s just one int).

Or, is there an even better way to do this? :D

EDIT: added breaks per Mat’s suggestion.

Thanks for all the responses, I am quite impressed with this community and its willingness to help everybody learn—you guys rock!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:57:12+00:00Added an answer on May 21, 2026 at 2:57 am

    Using a single variable to represent a set of boolean flags works well if they are related. In general, I’d avoid doing this if the flags were not related. However in your case where the 3 flags relate to the program and how it runs, I’d say this is a good use.

    As far as the implementation goes, rather than using hard coded constant values for your flags, you should define macros or an enum to represent the flags. That way it is clear that you are setting (or unsetting) which flag. Though it’s probably not necessary to use pointers here as you have in your code.

    e.g.,

    enum option
    {
        OPTION_none = 0,
        OPTION_a = 0x1,
        OPTION_b = 0x2,
        OPTION_c = 0x4,
    };
    
    enum option processOpts(int argc, char **argv)
    {
        enum option options = OPTION_none;
        if (argc > 1)
        {
            int i;
            for (i = 1; i < argc; i++)
            {
                if (argv[i][0] == '-')
                {
                    switch (argv[i][1])
                    {
                    case 'a': options |= OPTION_a; break;
                    case 'b': options |= OPTION_b; break;
                    case 'c': options |= OPTION_c; break;
                    }
                }
            }
        }
        return options;
    }
    
    int main(int argc, char *argv[])
    {
        enum option options = processOpts(argc, argv);
        if (options & OPTION_a)
        {
            /* do stuff for option a */
        }
        if (options & OPTION_b)
        {
            /* do stuff for option b */
        }
        if (options & OPTION_c)
        {
            /* do stuff for option c */
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some confusion with the new keyword,things work fine when I am
I'm having some fun trying to get my head around some MVP stuf, as
I'm having some issues understanding how to reference new browser windows after opening them.
I 'm relatively new to git and and having some problems early on. I've
I'm really new to CodeIgniter and am having some trouble getting started. I see
I'm new to rails and I'm having some trouble in the console. I'd like
I am having some trouble setting up FancyBox, I am somewhat new to jQuery.
I'm completely new to Visual Studio and I'm having some trouble getting a project
I'm very new to Tomcat and I'm having some issues figuring out how to
I am new to Aptana Studio and I'm having some difficulty with the 'Content

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.