Originally Posted by
lovesunset21
Thanks for your quick reply. I am working with memcopy function, but it displays a warning "implicit declaration of function ‘memcopy’". I don't know why
Why would you need memcpy?
First you've got to straighten out your bitfield definition...
Code:
typedef struct
{
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
unsigned b8:1;
} byte_s;
Your struct is going to be 32 bytes long.
Try it like this...
Code:
typedef struct t_byte_s
{ unsigned char b0:1,
b1:1,
b2:1,
b3:1,
b4:1,
b5:1,
b6:1,
b7:1; }
byte_s;
This will be only 1 byte long (although not all compilers will do this for a byte)
Now you need some means of putting a value in there in one form and getting it out in another. For that we have to use a union...
Code:
typedef union t_byte_s
{ unsigned char MyByte;
unsigned char b0:1,
b1:1,
b2:1,
b3:1,
b4:1,
b5:1,
b6:1,
b7:1; }
byte_s;
Ok, now you can do this...
Code:
byte_s testvar;
testvar.MyByte = 127;
if (testvar.b6)
puts ("eyup, it's a 1");
testvar.MyByte = 0; // clear the bits
testvar.b2 = 1;
printf("We got %d",testvar.MyByte); // = 4
You can feed in individual bits and read the byte or vice versa, hand it a byte and read the bits.
The reason this works is that in a union all variables occupy the same space (i.e. start at the same address).