D
David Resnick
I'm a bit confused about how bitfield organization works.
In the following code:
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
/* Layout of structure
* 7 6 5 4 3 2 1 0
*| PDU | Frame |
*| FQC | RFCI |
*| Header CRC | Pay
* load CRC |
i.e.
4 bits PDU
4 bits Frame
2 bits FQC
6 bits RFCI
6 bits Header CRC
10 bits Payload CRC
*/
int main(void)
{
unsigned int i;
typedef struct
{
uint8_t frame:4; // The frame number
uint8_t PDUType:4; // The PDU type, we support 0 for media
or 14 for ctrl
uint8_t RFCI:6; // Radio Access Bearer Sub Flow
Indicator (helpful?)
uint8_t FQC:2; // Frame Quality Classification
uint16_t HeaderCRC:6; // CRC for checking header integrity
uint16_t PayloadCRC:10; // CRC for checking payload integrity
} IU_UP_PDU_TYPE_0;
IU_UP_PDU_TYPE_0 blah;
memset(&blah, '\0', sizeof blah);
blah.HeaderCRC = 0x3F;
unsigned char *pBlah = (unsigned char*)&blah;
for ( i = 0; i < sizeof blah; ++i )
{
printf("%02X ", (unsigned int) pBlah);
}
printf("\n");
return 0;
}
I can't see a way to get the Header CRC to occupy the correct bits in
the overall arrangement.
temp(641)$ gcc -Wall -o foo foo.c
temp(642)$ foo
00 00 3F 00
As you can see, it occupies the 6 LSB of the uint16_t.
I gather bitfields aren't very portable. Any wisdom on the above?
Thanks,
-David
In the following code:
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
/* Layout of structure
* 7 6 5 4 3 2 1 0
*| PDU | Frame |
*| FQC | RFCI |
*| Header CRC | Pay
* load CRC |
i.e.
4 bits PDU
4 bits Frame
2 bits FQC
6 bits RFCI
6 bits Header CRC
10 bits Payload CRC
*/
int main(void)
{
unsigned int i;
typedef struct
{
uint8_t frame:4; // The frame number
uint8_t PDUType:4; // The PDU type, we support 0 for media
or 14 for ctrl
uint8_t RFCI:6; // Radio Access Bearer Sub Flow
Indicator (helpful?)
uint8_t FQC:2; // Frame Quality Classification
uint16_t HeaderCRC:6; // CRC for checking header integrity
uint16_t PayloadCRC:10; // CRC for checking payload integrity
} IU_UP_PDU_TYPE_0;
IU_UP_PDU_TYPE_0 blah;
memset(&blah, '\0', sizeof blah);
blah.HeaderCRC = 0x3F;
unsigned char *pBlah = (unsigned char*)&blah;
for ( i = 0; i < sizeof blah; ++i )
{
printf("%02X ", (unsigned int) pBlah);
}
printf("\n");
return 0;
}
I can't see a way to get the Header CRC to occupy the correct bits in
the overall arrangement.
temp(641)$ gcc -Wall -o foo foo.c
temp(642)$ foo
00 00 3F 00
As you can see, it occupies the 6 LSB of the uint16_t.
I gather bitfields aren't very portable. Any wisdom on the above?
Thanks,
-David