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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:50:01+00:00 2026-05-27T00:50:01+00:00

I was pondering (and therefore am looking for a way to learn this, and

  • 0

I was pondering (and therefore am looking for a way to learn this, and not a better solution) if it is possible to get an array of bits in a structure.

Let me demonstrate by an example. Imagine such a code:

#include <stdio.h>

struct A
{
    unsigned int bit0:1;
    unsigned int bit1:1;
    unsigned int bit2:1;
    unsigned int bit3:1;
};

int main()
{
    struct A a = {1, 0, 1, 1};
    printf("%u\n", a.bit0);
    printf("%u\n", a.bit1);
    printf("%u\n", a.bit2);
    printf("%u\n", a.bit3);
    return 0;
}

In this code, we have 4 individual bits packed in a struct. They can be accessed individually, leaving the job of bit manipulation to the compiler. What I was wondering is if such a thing is possible:

#include <stdio.h>

typedef unsigned int bit:1;

struct B
{
    bit bits[4];
};

int main()
{
    struct B b = {{1, 0, 1, 1}};
    for (i = 0; i < 4; ++i)
        printf("%u\n", b.bits[i]);
    return 0;
}

I tried declaring bits in struct B as unsigned int bits[4]:1 or unsigned int bits:1[4] or similar things to no avail. My best guess was to typedef unsigned int bit:1; and use bit as the type, yet still doesn’t work.

My question is, is such a thing possible? If yes, how? If not, why not? The 1 bit unsigned int is a valid type, so why shouldn’t you be able to get an array of it?

Again, I don’t want a replacement for this, I am just wondering how such a thing is possible.

P.S. I am tagging this as C++, although the code is written in C, because I assume the method would be existent in both languages. If there is a C++ specific way to do it (by using the language constructs, not the libraries) I would also be interested to know.

UPDATE: I am completely aware that I can do the bit operations myself. I have done it a thousand times in the past. I am NOT interested in an answer that says use an array/vector instead and do bit manipulation. I am only thinking if THIS CONSTRUCT is possible or not, NOT an alternative.

Update: Answer for the impatient (thanks to neagoegab):

Instead of

typedef unsigned int bit:1;

I could use

typedef struct
{
    unsigned int value:1;
} bit;

properly using #pragma pack

  • 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-27T00:50:03+00:00Added an answer on May 27, 2026 at 12:50 am

    NOT POSSIBLE – A construct like that IS NOT possible(here) – NOT POSSIBLE

    One could try to do this, but the result will be that one bit is stored in one byte

    #include <cstdint>
    #include <iostream>
    using namespace std;
    
    #pragma pack(push, 1)
    struct Bit
    {
        //one bit is stored in one BYTE
        uint8_t a_:1;
    };
    #pragma pack(pop, 1)
    typedef Bit bit;
    
    struct B
    {
        bit bits[4];
    };
    
    int main()
    {
        struct B b = {{0, 0, 1, 1}};
        for (int i = 0; i < 4; ++i)
            cout << b.bits[i] <<endl;
    
        cout<< sizeof(Bit) << endl;
        cout<< sizeof(B) << endl;
    
        return 0;
    }
    

    output:

    0 //bit[0] value
    0 //bit[1] value
    1 //bit[2] value
    1 //bit[3] value
    1 //sizeof(Bit), **one bit is stored in one byte!!!**
    4 //sizeof(B), ** 4 bytes, each bit is stored in one BYTE**
    

    In order to access individual bits from a byte here is an example (Please note that the layout of the bitfields is implementation dependent)

    #include <iostream>
    #include <cstdint>
    using namespace std;
    
    #pragma pack(push, 1)
    struct Byte
    {
        Byte(uint8_t value):
            _value(value)
        {
        }
        union
        {
        uint8_t _value;
        struct {
            uint8_t _bit0:1;
            uint8_t _bit1:1;
            uint8_t _bit2:1;
            uint8_t _bit3:1;
            uint8_t _bit4:1;
            uint8_t _bit5:1;
            uint8_t _bit6:1;
            uint8_t _bit7:1;
            };
        };
    };
    #pragma pack(pop, 1)
    
    int main()
    {
        Byte myByte(8);
        cout << "Bit 0: " << (int)myByte._bit0 <<endl;
        cout << "Bit 1: " << (int)myByte._bit1 <<endl;
        cout << "Bit 2: " << (int)myByte._bit2 <<endl;
        cout << "Bit 3: " << (int)myByte._bit3 <<endl;
        cout << "Bit 4: " << (int)myByte._bit4 <<endl;
        cout << "Bit 5: " << (int)myByte._bit5 <<endl;
        cout << "Bit 6: " << (int)myByte._bit6 <<endl;
        cout << "Bit 7: " << (int)myByte._bit7 <<endl;
    
        if(myByte._bit3)
        {
            cout << "Bit 3 is on" << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been pondering this problem for a while and can't find the solution (It
I'm not a big fan of XML files. Therefore I'm wondering if there is
everyone! new to here and been pondering about this myself for some times with
Wondering what the best / good way of doing this would be in jQuery.
I am looking at the JVM configuration options for running Alfresco, mainly this document
I'm looking into writing a wxWidget that displays a graphical node network, and therefore
I have a form looking like this: class MarketingActionForm(forms.ModelForm): contact = ManyToManyByLetter(Contact, field_name=first_name) #contact
I am looking to get some ideas on how I can solve my failover
I'm not sure if this is the right place to ask about hardware but
am looking to let the users of my web application define their own attributes

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.