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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:42:38+00:00 2026-05-18T20:42:38+00:00

Is there a more compact way of comparing my bits than this (the only

  • 0

Is there a more compact way of comparing my bits than this (the only way I know):

#define BIT1 1
#define BIT2 2
#define BIT3 4
#define BIT4 8
#define BIT5 16
#define BIT6 32

// I declare this somewhere in a structure
unsigned char bits: 6;

// I want all of them to be 0 at first (000000)
bits = 0;

/* I do some bite setting here */

// I only want to know if the state of my bits == 000000
if(bits & (BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6) == (BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6))
{
    // All kinds of nasty stuff
}

I thought maybe something in the lines of bits & 0x00 == 0x00

  • 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-18T20:42:38+00:00Added an answer on May 18, 2026 at 8:42 pm

    If you want compact (as indicated in your comment) rather than fast, why not do something like:

    #define BIT1 0x01
    #define BIT2 0x02
    #define BIT3 0x04
    #define BIT4 0x08
    #define BIT5 0x10
    #define BIT6 0x20
    #define BITS1THRU4 (BIT1|BIT2|BIT3|BIT4)
    // or #define BITS1THRU6 0x0f
    
    // I declare this somewhere in a structure
    unsigned char bits: 6;
    
    // I want all of them to be 0 at first (000000)
    bits = 0;
    
    /* I do some bit setting here */
    
    // I only want to know if the state of my first four bits == 0000
    if(bits & BITS1THRU4 == 0) ...
    

    It probably won’t be any faster since your original code would have been turned into that constant anyway but it may be more readable (which is often a good reason to do it).

    If you have a need for other variations, just define them. If there’s too many of them (63 defines, if you use them all, may be getting a bit on the high side), I’d start thinking about another solution.

    But, to be honest, unless you’re going to use more meaningful names for the defines, I’d just ditch them. The name BIT3 really adds nothing to 0x04 to those that understand bit patterns. If it was something like UART_READ_READY_BIT, that would be fine but what you have is only slightly better than:

    #define THREE 3
    

    (no offence intended, I’m just pointing out my views). I’d just work out the bit patterns and put them straight in the code (bits 1 thru 6 in your case being 0x3f).

    And, just as an aside, for you particular case, I think bits will only be those six bits anyway so you may find it’s enough to compare it to 0 (with no bit masking). I’ve left in the bit masking method in case you wanted a mode general solution for checking specific bits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more compact way of comparing my bits than this (the only
Is there a more compact and/or conventional way to write this? With ActiveSheet.QueryTables.Add(Connection:=URL;http://carbon.brighterplanet.com/flights.txt, Destination:=Range(A2))
Is there anything more compact (or otherwise better) than this? <x:Array x:Key=titles Type=System:String> <System:String>Mr.</System:String>
Is there any way to make code more compact for this situation? // pseudocode
I would like to know if there is a more compact (or Pythonic) way
Is there a more compact/efficient way of doing this? for key in kwargs: if
Is there a way to make this situation more compact in rails views? Eg
Is there a more compact way to do this sort of initialization? for (var
Is there a more efficent way of doing this in terms of memory usage
is there anything more comfortable than this to detect a special page? The page

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.