<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><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: hosking@cs.purdue.edu<br>> Date: Thu, 23 Aug 2012 09:30:57 -0400<br>> To: dragisha@m3w.org<br>> CC: m3devel@elegosoft.com<br>> Subject: Re: [M3devel] I know, I know...<br>> <br>> PACKED?<br>> <br>> Sent from my iPad<br>> <br>> On Aug 23, 2012, at 3:24 AM, Dragiša Durić <dragisha@m3w.org> wrote:<br>> <br>> > I know this will probably be very dense subject, but. What about:<br>> > <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 *) <br>> > cc: BITS 4 FOR Nibble;<br>> > END;<br>> > <br>> > Meaning: bit data is packed from left to right, and all multi-byte data is packed MSB first.<br>> > <br>> > Right now I am doing this, based on knowledge of my platform (little endian, x86_64), like this:<br>> > <br>> > TSPacketHeader = RECORD<br>> > sync: BITS 8 FOR [16_0..16_ff]; (* Always 0x47 *)<br>> > <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>> > <br>> > pidLo: BITS 8 FOR [16_00..16_ff];<br>> > <br>> > cc: BITS 4 FOR Nibble;<br>> > afc: BITS 2 FOR [0..3]; (* 01 - no adaptation field, payload only *) <br>> > transScramControl: BITS 2 FOR [0..3]; (* 00 means no scrambling *)<br>> > END;<br>> > <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>> > <br>> > Thanks in advance..<br>> > --<br>> > Divided by a common language<br>> > <br>> > Dragiša Durić<br>> > dragisha@m3w.org<br>> > <br>> > <br>> > <br>> > <br></div> </div></body>
</html>