07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>MPLAB</strong> C <strong>Compiler</strong> <strong>for</strong> PIC18 <strong>MCUs</strong> vs. 16-Bit Devices<br />

D.21 BIT FIELDS<br />

Bit fields in the PIC18 MCU <strong>Compiler</strong> cannot cross byte storage boundaries <strong>and</strong>, there<strong>for</strong>e,<br />

cannot be greater than 8 bits in size.<br />

The 16-Bit Device <strong>Compiler</strong> supports bit fields with any bit size, up to the size of the<br />

underlying type. Any integral type can be made into a bit field. The allocation cannot<br />

cross a bit boundary natural to the underlying type.<br />

For example:<br />

struct foo {<br />

long long i:40;<br />

int j:16;<br />

char k:8;<br />

} x;<br />

struct bar {<br />

long long I:40;<br />

char J:8;<br />

int K:16;<br />

} y;<br />

struct foo will have a size of 10 bytes using the 16-Bit Device <strong>Compiler</strong>. i will be<br />

allocated at bit offset 0 (through 39). There will be 8 bits of padding be<strong>for</strong>e j, allocated<br />

at bit offset 48. If j were allocated at the next available bit offset, 40, it would cross a<br />

storage boundary <strong>for</strong> a 16 bit integer. k will be allocated after j, at bit offset 64. The<br />

structure will contain 8 bits of padding at the end to maintain the required alignment in<br />

the case of an array. The alignment is 2 bytes because the largest alignment in the<br />

structure is 2 bytes.<br />

struct bar will have a size of 8 bytes using the 16-Bit Device <strong>Compiler</strong>. I will be allocated<br />

at bit offset 0 (through 39). There is no need to pad be<strong>for</strong>e J because it will not<br />

cross a storage boundary <strong>for</strong> a char. J is allocated at bit offset 40. K can be allocated<br />

starting at bit offset 48, completing the structure without wasting any space.<br />

© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 209

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!