Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of Bit_fields in C

Tags:

c

bit-fields

I've a flag of 8 bits in C and I want to access it bit by bit using bit-fields like this:

#include <stdio.h>
#include <stdint.h>

int main(void) {
    struct flags{
        uint8_t bits1:1;
        uint8_t bits2:1;
        uint8_t bits3:1;
        uint8_t bits4:1;
        uint8_t bits5:1;
        uint8_t bits6:1;
        uint8_t bits7:1;
        uint8_t bits8:1;
    };
    struct flags *my_flags;
    uint8_t x=6,i;

    my_flags=(struct flags *)&x;
    printf("%u\t",my_flags->bits5);
    printf("%u\t",my_flags->bits6);
    printf("%u\t",my_flags->bits7);
    printf("%u\t",my_flags->bits8);
    printf("%u\t",my_flags->bits1);
    printf("%u\t",my_flags->bits2);
    printf("%u\t",my_flags->bits3);
    printf("%u\t",my_flags->bits4);
    return 0;
}

and I get the expected output: 0 0 0 0 0 1 1 0.

But this is a bit too much coding.

  1. Isn't there something like array of bit-fields or any other workaround for it? (or)
  2. Is there anything like this in C my_flags->bits_i where i will be a counter in a loop?

I know both are not there in C by default. But are there any alternatives to achieve the same?

like image 805
RatDon Avatar asked Oct 31 '14 05:10

RatDon


People also ask

Can we have array of bit fields?

Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components.

What is Bit Array in C?

A bit array is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level parallelism in hardware to perform operations quickly.

What is bit fields in C?

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared.

Can we have an array of bit fields a yes b no?

No, you can't. Bit field can only be used with integral type variables.


2 Answers

You can use an union with an anonymous inner structure (C11):

union flags
{
    unsigned char u;
    struct
    {
        uint8_t bits1:1;
        uint8_t bits2:1;
        uint8_t bits3:1;
        uint8_t bits4:1;
        uint8_t bits5:1;
        uint8_t bits6:1;
        uint8_t bits7:1;
        uint8_t bits8:1;
    };
};

That you can use for example this way:

union flags x = {0x42};

for (i = CHAR_BIT - 1; i >= 0; i--)
{
    printf("%d\t", (x.u >> i) & 1);
}

printf("\n");

and to access a specific bit:

x.bits8 = 1;
printf("%d\n", x.bits8);

An anonymous inner structure is also allowed as GNU extension to C89 and C99.

like image 161
ouah Avatar answered Oct 16 '22 02:10

ouah


  1. No. You can't create an array of bit-field
  2. You can extract each bit by using >> and &

    int main() {
        int i, x=45;
        for(i=0; i<8; i++) {
            printf("%d", (x>>(7-i))&1);
        }
    }
    
like image 30
Rakesh Malik Avatar answered Oct 16 '22 00:10

Rakesh Malik