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 7936467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:13:28+00:00 2026-06-03T22:13:28+00:00

I am trying represent a bunch of values in a single char: 6 on/off

  • 0

I am trying represent a bunch of values in a single char: 6 on/off flags in the first 6 bits, and using the last 2 bits to hold 4 different values.
This seems so basic that macros like this must exist somewhere, but I can’t find them.

 #define HOT 0x00
 #define BIG 0x01
 #define FAT 0x02
 #define HIT 0x03
 #define BAT 0x04
 #define ZAX 0x05    
 #define HOW 0x06

 #define TWO_BITS     nnn    // ???
 #define CONDITION_0  nnn   // bit combo: 00
 #define CONDITION_1  nnn   // bit combo: 01
 #define CONDITION_2  nnn   // bit combo: 10
 #define CONDITION_3  nnn   // bit combo: 11

void bitter(void)
{
    unsigned char myBits = 0;
    bool nonsense;

    if (myBits & BIG)  nonsense = true;    // BIG flag on   
    if (!(myBits & BAT)) nonsense = false; // BAT flag off

    myBits = myBits | BIG;     // turn on BIG bit
    myBits = myBits & ~BIG;    // turn off BIG bit

    if (TWO_BITS == CONDITION_0)
        doThis();
    else if (TWO_BITS == CONDITION_1_)
        doThat();
    // ... etc

}

So what is the best way to code what I want to do with those last 2 bits?
I haven’t been able to do any performance testing because I haven’t figured out how to write the code, but I’m assuming this is the fastest way to do these sort of ops.

[btw, this may smell like homework, but I’m just a 54 year old dog trying to learn some new tricks.]

  • 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-06-03T22:13:31+00:00Added an answer on June 3, 2026 at 10:13 pm

    It sort of depends on whether you need to deal with those 2 bits on its own, as a (decimal) value between 0 and 3, or if you always treat them as the upper 2 bits of a byte.

    Here’s one way, we just mask out all the other bits, and define the conditions as the value those 2 bits will make as the upper 2 bits in a byte.

     #define TWO_BITS(x) ((x) & 0xC0)
    
     #define CONDITION_0  0
     #define CONDITION_1  0x40   // bit combo: 01
     #define CONDITION_2  0x80   // bit combo: 10
     #define CONDITION_3  0xC0   // bit combo: 11
    

    That is, the upper 2 bits of a byte is the binary 1 1 0 0 0 0 0 0 , which is 0xC0 in hex. And the upper 2 bits being 0 1 , i.e. with all the bits in a byte it would be 0 1 0 0 0 0 0 0 , which is 0x40 in hex.

    And your test would have to be

    if (TWO_BITS(myBits) == CONDITION_0)
    

    The other approach is to extract those upper 2 bits as a 2 bit integer (that is, a value between 0 and 3). That’s easy, just shift the bits 6 places to the right.

     #define TWO_BITS(x) ((x) >> 6)
    
     #define CONDITION_0  0x0
     #define CONDITION_1  0x01   // bit combo: 01
     #define CONDITION_2  0x02   // bit combo: 10
     #define CONDITION_3  0x03   // bit combo: 11
    

    The usage would be the same when testing one of the conditions.

     if (TWO_BITS(myBits) == CONDITION_0)
    

    A last note, it seems your lower 6 bit flags is a bit wrong, consider e.g.

    #define HOW 0x06
    

    0x06 is the binary value 0 0 0 0 0 1 1 0 , so that’s actually turning on or testing for 2 bits. You likely want to associate 1 bit with 1 flag, which would be this sequence

     #define BIG 0x01
     #define FAT 0x02
     #define HIT 0x04
     #define BAT 0x08
     #define ZAX 0x10
     #define HOW 0x20
    

    This is often written as a bit shift so it’s easy to read which bit it is:

     #define BIG (1 << 0)  //bit zero
     #define FAT (1 << 1)  //bit 1
     #define HIT (1 << 2)  //bit 2
     #define BAT (1 << 3)  //bit 3 , etc.
     #define ZAX (1 << 4)
     #define HOW (1 << 5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to represent a 2D array using tbb::concurrent_vector<T> . This 2d
I am trying to represent a graph using disjoint union and record. The following
I am trying to make a class that will represent two different behaivours at
I've been trying to make a generic class to represent a range of values
I am trying to represent the maximum 64-bit unsigned value in different bases. For
I am trying to represent a tree data-structure in F# using the following type:
I am trying to represent the data on this ternary graph for lookup. I
I am trying to represent a hierarchy using namedtuple . Essentially, every node has
I am trying to represent a graph with typed edges in an entity-framework code-first
I am trying to represent to genetic variation data in a database for my

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.