<html><head><base href="x-msg://148/"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>You mean, for system programming I have to use C?</div><div><br></div><div apple-content-edited="true">
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>--</div><div>Divided by a common language</div><div><br></div><div>Dragiša Durić</div><div><a href="mailto:dragisha@m3w.org">dragisha@m3w.org</a></div><div><br></div></div></span><br class="Apple-interchange-newline"></span><br class="Apple-interchange-newline">
</div>
<br><div><div>On Aug 23, 2012, at 9:26 PM, Jay K wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div class="hmmessage" style="font-size: 12pt; font-family: Calibri; "><div dir="ltr">How about you just use "bytes" or integers and helper functions like this:<br> <br> <br>typedef struct _BIT_FIELD {<br> UINT8 ByteOffset;<br> UINT8 BitOffset;<br> UINT8 BitCount;<br>} BIT_FIELD;<br><br> <br>HRESULT<br>ExtractBitField(__in const BIT_FIELD* BitField,<br> __in const UINT8* Bytes,<br> __in size_t Size,<br> __out_opt UINT8* Value)<br>/*++<br>Extract a bitfield from an array of bytes, as specified<br> by a byte offset, a bit offset within the first byte, and a bit count.<br>The bitfield is presumed to be no more than 8 bits -- however that could be easily fixed.<br>The bitfield can definitely cross byte boundaries.<br>--*/<br>{<br> UINT8 const bitCount = BitField->BitCount;<br> UINT8 const bitOffset = BitField->BitOffset;<br> UINT8 const byteOffset = BitField->ByteOffset;<br> UINT8 const FF = (UINT8)~(UINT8)0;<br> BOOL8 const twoBytes = ((bitOffset + bitCount) > 8);<br> UINT8 const secondBitOffset = (8 - bitOffset);<br> UINT8 localValue = { 0 };<br> HRESULT error = { 0 };<br> if (Value)<br> *Value = 0;<br> if ((bitCount > 8) || (bitOffset > 7) || (bitCount == 0))<br> {<br> error = E_INVALIDARG;<br> goto Exit;<br> }<br> if ((byteOffset + twoBytes) >= Size)<br> {<br> error = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);<br> goto Exit;<br> }<br> localValue = (Bytes[byteOffset] >> bitOffset);<br> if (twoBytes)<br> {<br> localValue &= ~(FF << secondBitOffset);<br> localValue |= (Bytes[byteOffset + 1] << secondBitOffset);<br> }<br> localValue &= ~(FF << bitCount);<br> error = 0;<br> if (Value)<br> *Value = localValue;<br>Exit:<br> return error;<br>}<br><br> <br>arrays of bytes are really the only way to control the layout.<br>Look at how GNU binutils works, for example...<br>Bitfields in C don't yield predictable/portable layout either.<br> <br> <br> - Jay<br> <br><div><div id="SkyDrivePlaceholder"></div>> From:<span class="Apple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>> Date: Thu, 23 Aug 2012 09:30:57 -0400<br>> To:<span class="Apple-converted-space"> </span><a href="mailto:dragisha@m3w.org">dragisha@m3w.org</a><br>> CC:<span class="Apple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>> Subject: Re: [M3devel] I know, I know...<br>><span class="Apple-converted-space"> </span><br>> PACKED?<br>><span class="Apple-converted-space"> </span><br>> Sent from my iPad<br>><span class="Apple-converted-space"> </span><br>> On Aug 23, 2012, at 3:24 AM, Dragiša Durić <<a href="mailto:dragisha@m3w.org">dragisha@m3w.org</a>> wrote:<br>><span class="Apple-converted-space"> </span><br>> > I know this will probably be very dense subject, but. What about:<br>> ><span class="Apple-converted-space"> </span><br>> > TSPacketHeader = <* ENDIAN = BIG*>RECORD<br>> > sync: BITS 8 FOR [16_0..16_ff]; (* Always 0x47 *)<br>> > tErrInd, (* Transport Error Indicator *)<br>> > pusi: BITS 1 FOR BOOLEAN; (* Payload Unit Start Indicator *)<br>> > transPrio: BITS 1 FOR [0..1];<br>> > pid: BITS 13 FOR PID;<br>> > transScramControl: BITS 2 FOR [0..3]; (* 00 means no scrambling *)<br>> > afc: BITS 2 FOR [0..3]; (* 01 - no adaptation field, payload only *)<span class="Apple-converted-space"> </span><br>> > cc: BITS 4 FOR Nibble;<br>> > END;<br>> ><span class="Apple-converted-space"> </span><br>> > Meaning: bit data is packed from left to right, and all multi-byte data is packed MSB first.<br>> ><span class="Apple-converted-space"> </span><br>> > Right now I am doing this, based on knowledge of my platform (little endian, x86_64), like this:<br>> ><span class="Apple-converted-space"> </span><br>> > TSPacketHeader = RECORD<br>> > sync: BITS 8 FOR [16_0..16_ff]; (* Always 0x47 *)<br>> ><span class="Apple-converted-space"> </span><br>> > pidHi: BITS 5 FOR [16_00..16_1f];<br>> > transPrio: BITS 1 FOR [0..1];<br>> > pusi,<br>> > tErrInd: BITS 1 FOR BOOLEAN;<br>> ><span class="Apple-converted-space"> </span><br>> > pidLo: BITS 8 FOR [16_00..16_ff];<br>> ><span class="Apple-converted-space"> </span><br>> > cc: BITS 4 FOR Nibble;<br>> > afc: BITS 2 FOR [0..3]; (* 01 - no adaptation field, payload only *)<span class="Apple-converted-space"> </span><br>> > transScramControl: BITS 2 FOR [0..3]; (* 00 means no scrambling *)<br>> > END;<br>> ><span class="Apple-converted-space"> </span><br>> > And please don't tell me "write in C", because then I will just "offload" this problem to C preprocessor and still only hope for the best. Modula-3 is provably very adept to systems programming and I hope it can be more so.<br>> ><span class="Apple-converted-space"> </span><br>> > Thanks in advance..<br>> > --<br>> > Divided by a common language<br>> ><span class="Apple-converted-space"> </span><br>> > Dragiša Durić<br>> > <a href="mailto:dragisha@m3w.org">dragisha@m3w.org</a><br>> ><span class="Apple-converted-space"> </span><br>> ><span class="Apple-converted-space"> </span><br>> ><span class="Apple-converted-space"> </span><br>> ></div></div></div></span></blockquote></div><br></body></html>