Hi all,
Have a usage of the following structures(with bit fields), after typecasting to the similar datasize structure and accessing 2 8bits as single 16bit datamember is resulting incorrect(swapping of nibbles).
What is the cause,
I think endianess should not be issue, as data assignment and accessing is on same machine, please do know how to fix this,
Host machine: Linux 64 Bit.
2.6.32-131.0.15.el6.x86_64 #1 SMP Tue May 10 15:42:40 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux
Code:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int UINT32;
typedef struct
{
UINT32 SigName:8; /* member of MG_EVT_NAME_ENUM */
UINT32 SigCnxn:8; /* number of Connection target (IX_NULL if none) */
UINT32 SigParm:16; /* signal parameter value */
} MG_SIGNAL_VECTOR_T32;
typedef struct
{
UINT32 RqEvtName:8; /* member of MG_EVT_NAME_ENUM */
UINT32 RqEvtCnxn:8; /* number of Connection target (IX_NULL if none) */
UINT32 RqEvtActn:8; /* action(s) OR of MG_EVT_ACTION_ENUMs */
UINT32 RqEvtSetn:8; /* set # of embedded EVENT SET (IX_NULL if none) */
} MG_RQEVT_VECTOR_T32;
main()
{
MG_RQEVT_VECTOR_T32 * pEvt = malloc(sizeof(MG_RQEVT_VECTOR_T32));
memset (pEvt, 0, sizeof(MG_RQEVT_VECTOR_T32));
pEvt->RqEvtActn = 0;
pEvt->RqEvtSetn |= 0xff;
printf("RqEvtName =%d, RqEvtCnxn =%d, RqEvtActn=%d, RqEvtSetn=%d\n", pEvt->RqEvtName, pEvt->RqEvtCnxn, pEvt->RqEvtActn, pEvt->RqEvtSetn);
MG_SIGNAL_VECTOR_T32 * pEvt1 = (MG_SIGNAL_VECTOR_T32 *)pEvt;
printf("SigName =%d, SigCnxn=%d, SigParm=%d\n",pEvt1->SigName, pEvt1->SigCnxn, pEvt1->SigParm);
}
./a.out
RqEvtName =0, RqEvtCnxn =0, RqEvtActn=0, RqEvtSetn=255
SigName =0, SigCnxn=0, SigParm=65280
Expecting the sigparam to have 255...
Thanks much
Santosh