On 4/5/06, Devendra Mulakkayala <dmulakkayala@xxxxxxxxxxxx> wrote: > > Hello, > > ISSUE: Facing problem with bit fields, when i am compiling my code with > GCC. It was previously compiled with diab compiler and was working fine. > > In our code Union definition is as follows..., > union > { > unsigned_8 indicator; > struct > { > unsigned_8 unused : 6; > unsigned_8 speed :1; > unsigned_8 time :1; > }bit; > }msg; > > In code, speed and time bits are set as follows > > msg.bit.speed=TRUE; > msg.bit.time=TRUE; > > Some where in code there is check for indicator as follows > > if(indicator==03) /* this is to check whether speed and time bits are set. I feel this is not what you should be doing. you should be checking if (msg.bit.speed== TRUE && msg.bit.time==TRUE) not what bits get set.The compiler can translate them to the correct C0 or 03. Don't you think so ? > If yes go iside block */ > { > ---- > ---- > ---- > } > > unused 6 bits are initialized to 0. > > This code previously compiled with diab compiler and working fine. Now i am > trying to compile with GCC compiler. > Your previous compiler was just giving you the wrong illusions . > The problem here is > 1. If speed and time bits are set, these bits are coming in MSB. That is it > is coming 1100 0000 ( C0 ) instead of 0000 0011 (03 ). That why eventhough > speed and time bits are set, condition is not satisfying because indicator > value is 'C0' not '03'. > 2. Compiler is assigning the memory from bottom to top for bit fields in > the structure. Previously in diab compiler it is aligning bits from top to > bottom, with that our code was working fine. > 2. I know that these bit fields are compiler dependent. Is there any option > in GCC to set the bit fields from top to bottom ( MSB to LSB ) > in structure. > 3. As per the client requirement we are not supposed to change the code. I > am looking for any option or any other way to solve this problem. > > Please help me in this > > Thanks.... > Mulakkayala > >