This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Bitfield Array

Hello to everyone,
I want to my ARM7 to access a (const)bitfield array, but as i'am absolute beginner not sure about C-syntax. My textbook don't includes Bitfield-Array.
Hope someone takes a look at this:

struct
{
unsigned char red[32]                   :1;
unsigned char orange[32]                :1;
unsigned char yellow[32]                :1;
unsigned char green[32]                 :1;
unsigned char blue[32]                  :1;
unsigned char violett[32]               :1;
} my_color;
const my_color.red[]    ={1,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1,0,1,1};
const my_color.orange[] ={0,1,0,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,0,1,1,1,1,1,1,0,1,1,1,0,0,1};
const my_color.yellow[] ={1,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1,0,1,1};
const my_color.green[]  ={0,1,0,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,0,1,1,1,1,1,1,0,1,1,1,0,0,1};
const my_color.blue[]   ={1,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1,0,1,1};
const my_color.violett[]={0,1,0,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,0,1,1,1,1,1,1,0,1,1,1,0,0,1};
int main (void)
{
        int i;

        for(i=0;i<32;i++)
        {
                printf("%u ",my_color.red[i]);
                printf("%u ",my_color.orange[i]);
                printf("%u ",my_color.yellow[i]);
                printf("%u ",my_color.green[i]);
                printf("%u ",my_color.blue[i]);
                printf("%u ",my_color.violett[i]);
                printf("%u\n",i);
        }
        while(1){}


first as i tried some syntax-variations i just got normal errormessages or programms didn't what i expected to do. But suddenly after some time my programm makes CA stop with a WINDOWS errormessage
:ca.exe hat Fehler verursacht und wird geschlossen. Starten Sie das Programm neu. Ein Fehlerprotokoll wird erstellt. OK
How can i bring my little blinky-toy to the water?
:-)

Parents
  • C does not have bit arrays. A small change gets you closer (the formatting renders in the preview, but might not after posting):

    #define HEX__(n) 0x##n##LU
    #define B8__(x) (((x&0x0000000FLU)?1:0) + \ 
                     ((x&0x000000F0LU)?2:0) + \ 
                     ((x&0x00000F00LU)?4:0) + \ 
                     ((x&0x0000F000LU)?8:0) + \ 
                     ((x&0x000F0000LU)?16:0)+ \ 
                     ((x&0x00F00000LU)?32:0)+ \ 
                     ((x&0x0F000000LU)?64:0)+ \ 
                     ((x&0xF0000000LU)?128:0))
    #define B8(d) ((unsigned char)B8__(HEX__(d)))
    #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24)+ \ 
                                    ((unsigned long)B8( db2)<<16)+ \ 
                                    ((unsigned long)B8( db3)<<8) + \ 
                                                    B8(dlsb))
    struct
    {
        unsigned long red;
        unsigned long orange;
        unsigned long yellow;
        unsigned long green;
        unsigned long blue;
        unsigned long violett;
    } my_color;
    const my_color.red    = B32(10111100,11110111,11111010,01111011);
    const my_color.orange = B32(01011100,11110100,11011111,10111001);
    const my_color.yellow = B32(10111100,11110111,11111010,01111011);
    const my_color.green  = B32(01011100,11110100,11011111,10111001);
    const my_color.blue   = B32(10111100,11110111,11111010,01111011);
    const my_color.violett= B32(01011100,11110100,11011111,10111001);
    int main (void)
    {
        int i;
        unsigned long mask = 0x80000000;
    
        for(i=0;i<32;i++)
        {
            printf("%c ",(my_color.red     & mask) ? '1' : '0');
            printf("%c ",(my_color.orange  & mask) ? '1' : '0');
            printf("%c ",(my_color.yellow  & mask) ? '1' : '0');
            printf("%c ",(my_color.green   & mask) ? '1' : '0');
            printf("%c ",(my_color.blue    & mask) ? '1' : '0');
            printf("%c ",(my_color.violett & mask) ? '1' : '0');
            printf("%c\n",i);
        }
        while(1){}
    }
    

Reply
  • C does not have bit arrays. A small change gets you closer (the formatting renders in the preview, but might not after posting):

    #define HEX__(n) 0x##n##LU
    #define B8__(x) (((x&0x0000000FLU)?1:0) + \ 
                     ((x&0x000000F0LU)?2:0) + \ 
                     ((x&0x00000F00LU)?4:0) + \ 
                     ((x&0x0000F000LU)?8:0) + \ 
                     ((x&0x000F0000LU)?16:0)+ \ 
                     ((x&0x00F00000LU)?32:0)+ \ 
                     ((x&0x0F000000LU)?64:0)+ \ 
                     ((x&0xF0000000LU)?128:0))
    #define B8(d) ((unsigned char)B8__(HEX__(d)))
    #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24)+ \ 
                                    ((unsigned long)B8( db2)<<16)+ \ 
                                    ((unsigned long)B8( db3)<<8) + \ 
                                                    B8(dlsb))
    struct
    {
        unsigned long red;
        unsigned long orange;
        unsigned long yellow;
        unsigned long green;
        unsigned long blue;
        unsigned long violett;
    } my_color;
    const my_color.red    = B32(10111100,11110111,11111010,01111011);
    const my_color.orange = B32(01011100,11110100,11011111,10111001);
    const my_color.yellow = B32(10111100,11110111,11111010,01111011);
    const my_color.green  = B32(01011100,11110100,11011111,10111001);
    const my_color.blue   = B32(10111100,11110111,11111010,01111011);
    const my_color.violett= B32(01011100,11110100,11011111,10111001);
    int main (void)
    {
        int i;
        unsigned long mask = 0x80000000;
    
        for(i=0;i<32;i++)
        {
            printf("%c ",(my_color.red     & mask) ? '1' : '0');
            printf("%c ",(my_color.orange  & mask) ? '1' : '0');
            printf("%c ",(my_color.yellow  & mask) ? '1' : '0');
            printf("%c ",(my_color.green   & mask) ? '1' : '0');
            printf("%c ",(my_color.blue    & mask) ? '1' : '0');
            printf("%c ",(my_color.violett & mask) ? '1' : '0');
            printf("%c\n",i);
        }
        while(1){}
    }
    

Children