I have a struct of the following type
typedef struct
{
unsigned int a : 8;
unsigned int b : 6;
unsigned int c : 2;
}x, *ptr;
What i would like to do, is change the value of field c.
I do something like the following
x structure = { 0 };
x->c = 1;
When I look at the memory map, I expect to find 00 01, but instead I find 00 40.
It looks like when arranging the second byte, it puts c field in the lowest bits and b field in the highest bits. I've seen this on both GCC and Windows compilers.
For now, what I do is the following, which is working OK.
unsigned char ptr2 = (unsigned char*) ptr
*(ptr2 + 1) &= 0xFC
*(ptr2 + 1) |= 0x01
Am I looking at the memory map wrong?
Thank you for your help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…