cancel
Showing results for 
Search instead for 
Did you mean: 

Word Alignment Issue

philipgillespie
Associate II
Posted on April 20, 2011 at 10:48

Word Alignment Issue

4 REPLIES 4
Andrew Neil
Evangelist
Posted on May 17, 2011 at 14:32

''Can someone explain how to the 14bits is alligned in the 16bit word?''

No - because you have not mentioned what compiler you're using!

This stuff is entirely compiler-dependent! 

 

''How can i assign these 14bits to the 14bit field''

 

Just do it ''manually'' with the normal 'C' shift and mask operators!

It is unlikely that using bitfileds will gain you any advantage, and you have already seen that it brings in plenty of issues...!
philipgillespie
Associate II
Posted on May 17, 2011 at 14:32

I'm using the GCC compiler with the Ride7 IDE.

As all the registers on my DAC chip are 24bits long, it therefore seemed the logical approach to use bitfields.

I ideally want my 14bit word to sit right-alligned (ie in bits 13:0).  Can you give code example of how you would extract this from an uint16_t and assign to data_u16 field please?

Cheers

domen23
Associate II
Posted on May 17, 2011 at 14:32

You usually want to avoid bitfields because they give you no guarantees on alignment and padding.

<<, >>, &, |, and ~ are your friends

Andrew Neil
Evangelist
Posted on May 17, 2011 at 14:32

''As all the registers on my DAC chip are 24bits long, it therefore seemed the logical approach to use bitfields.''

In theory, perhaps.

But, in practice, 'C' bitfields are so compiler-dependent that it's not worth the effort - and there's unlikely to be any benefit anyhow.

''I ideally want my 14bit word to sit right-alligned (ie in bits 13:0).  Can you give code example of how you would extract this from an uint16_t and assign to data_u16 field please?''

Example of what, exactly?

Why would there be any need to ''extract'' anything?

You just ignore the excess bits...

Are familiar with the standard bitwise operators, and the shift operators, in 'C'...?