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

Hello to everyone,
i have this example:

union
{
  struct
  {
    unsigned char x:4;
    unsigned char y:4;
  } nibble;
  unsigned char abyte;
} value;


I'am not sure how to acces to the byte and the two nibble's.
I tried several hours, maybe there's a mistake in my textbook?
Or there is a fatal error in my understanding of struct and union's!

There's an really idioticall way to solve this:
Just toggling thru all permutations of syntax :-(
No, better to ask someone who knows and is willing to help :-)

Parents
  • Not sure what your question is exactly but this might help.

    union
    {
      struct
      {
        unsigned char x:4;
        unsigned char y:4;
      } nibble;
      unsigned char abyte;
    } value;
    
    typedef union
    {
      struct
      {
        unsigned char x:4;
        unsigned char y:4;
      } nibble;
      unsigned char abyte;
    } tyvalue;
    
    int main(void)
    {
       tyvalue myvalue;
    
       value.abyte = 0x12;
       value.nibble.x = 0xF;
       value.nibble.y = 0xF;
    
       myvalue.abyte = 0x12;
       myvalue.nibble.x = 0xF;
       myvalue.nibble.y = 0xF
    

Reply
  • Not sure what your question is exactly but this might help.

    union
    {
      struct
      {
        unsigned char x:4;
        unsigned char y:4;
      } nibble;
      unsigned char abyte;
    } value;
    
    typedef union
    {
      struct
      {
        unsigned char x:4;
        unsigned char y:4;
      } nibble;
      unsigned char abyte;
    } tyvalue;
    
    int main(void)
    {
       tyvalue myvalue;
    
       value.abyte = 0x12;
       value.nibble.x = 0xF;
       value.nibble.y = 0xF;
    
       myvalue.abyte = 0x12;
       myvalue.nibble.x = 0xF;
       myvalue.nibble.y = 0xF
    

Children
No data