Simon Chamlian

Bitfileds on 5282

Discussion created by Simon Chamlian on Aug 26, 2008
Latest reply on Aug 27, 2008 by CrasyCat
Hi,
 
Having hard time with bit fields. In the following snippets,
 
Code:
//-----------------------------------------------typedef unsigned short int      uint16//-----------------------------------------------typedef struct { unsigned int source : 5;  unsigned int msg_id:  3;  unsigned int msg_type: 1;  unsigned int request_type: 1;  unsigned int data_type: 6; } BitFields_T;//-----------------------------------------------typedef union { BitFields_T b; uint16 word16;} HEADER_T;//-----------------------------------------------int main(void){  HEADER_T Var; Var.word16=0;  Var.b.source=1;}

 
I was expecting to have Var.word16==1. Instead I have Var.word16==2048 (shifted by 12 ).
 
Any hints?
 
Thanks,
S.

 

Outcomes