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

  • Home
  • SEARCH
  • 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 9312717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:41:50+00:00 2026-06-19T01:41:50+00:00

Suppose I have a byte structure, like this : struct one_byte { char b1

  • 0

Suppose I have a byte structure, like this :

struct one_byte
{
char b1 : 1,
     b2 : 1,   
     b3 : 1,   
     b4 : 1,   
     b5 : 1,   
     b6 : 1,   
     b7 : 1,   
     b8 : 1;   
}foo;

In some cases I’ll need to check (foo == 0), then I have to do eight commands :

if(foo.b1 == 0 &&
   foo.b2 == 0 &&
   foo.b3 == 0 &&
...and so on

Is there any portable & convenient way which can instantly check zero value only with a single command? I tried functions & templates, they perform very slowly. And I tried union, my compiler doesn’t support bit[array]….

  • 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-19T01:41:51+00:00Added an answer on June 19, 2026 at 1:41 am

    Use union, this is what it is intended for

    union {
      struct {
        char b1:1,b2:1,b3:1,b4:1,b5:1,b6:1,b7:1,b8:1; 
      } bits;
      unsigned char byte;
    } u;
    

    then you can either assign directly the byte

    u.byte = 15;
    

    or the bits individually

    u.bits.b3 = 1;
    

    Exemple

    int main() {
      u.byte = 0;
      printf("%x\n", u.byte);
      u.bits.b3 = 1;
      u.bits.b4 = 1;
      printf("%x\n", u.byte);
      return 0;
    }
    

    will output

    0
    c  // 12 in decimal, since b3 and b4 are set to 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this question: suppose that the size of char is one byte and
Suppose I have a C# struct like this: [StructLayout(LayoutKind.Explicit)] struct IMAGE_DOS_HEADER { [FieldOffset(60)] public
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms
Suppose we have a code block like this : //Assuming s is a stream:
Suppose we have a packet struct Foo { short size; // 2 short type;
Suppose you have a string like €foo\xA0 , encoded UTF-8, Is there a way
This is just an idea, I don't yet have any code, I need some
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a Byte array and I use Encoding.ASCII.GetString() to convert the bytes

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.