From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,91e38895ea853f4b,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-10-26 09:50:43 PST Message-ID: <3BD99406.62B13405@Raytheon.com> From: Mark Johnson X-Mailer: Mozilla 4.5 [en] (WinNT; I) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Query on portable bit extraction Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Fri, 26 Oct 2001 11:49:11 -0500 NNTP-Posting-Host: 192.27.48.44 X-Complaints-To: news@ext.ray.com X-Trace: dfw-service2.ext.raytheon.com 1004114972 192.27.48.44 (Fri, 26 Oct 2001 11:49:32 CDT) NNTP-Posting-Date: Fri, 26 Oct 2001 11:49:32 CDT Organization: Raytheon Company Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!out.nntp.be!propagator-SanJose!in.nntp.be!easynews!news.city-guide.com!attdl1!attdl2!attsl2!attla2!attla1!ip.att.net!bos-service1.ext.raytheon.com!dfw-service2.ext.raytheon.com.POSTED!not-for-mail Xref: archiver1.google.com comp.lang.ada:15263 Date: 2001-10-26T11:49:11-05:00 List-Id: We have code that we want to run on both a big (sgi) and little (pc) endian machine. As an example, the layout of the data in memory is... [big_endian bit order] Byte 0 1 2 3 4 5 6 7 21 - X X X Y Y Y Y Y 22 - Y Y Z Z Z Z Z Z where we need to extract XXX as the "wait code", YYYYYYY as the "value", and ZZZZZZ is a pad area (don't care). On the big endian machine it was coded like this... Wait := (byte 21) / 2**5; T1 := (move bytes from bytes 21 & 22) T2 := T1 / 2**6; T1 := T2 / 2**7; T1 := T1 * 2**7; Value := T2-T1; which won't work on the PC unless we swap the bytes in the first assignment to T1 and looks ugly as all get out. Thinking of records & representation specifications, I'd like to be able to do something like this... subtype Wv is Natural range 0..7; subtype Vv is Natural range 0..127; subtype Pv is Natural range 0..63; type Wait_Value is record Wait : Wv; Value : Vv; Pad : Pv; end record; for Wait_Value use record Wait at 0 range 0..2; Value at 0 range 3..9; Pad at 1 range 2..7; end record; for Wait_Value'Bit_Order use System.High_Order_First; which looks pretty reasonable until I get the following GNAT error messages on the PC... error: attempt to specify non-contiguous field not permitted error: (caused by non-standard Bit_Order specified) Hmm. After drawing it out on the white board - sure enough, the bits are not contiguous, a byte swap is needed. Assuming I do a byte swap somewhere, I recode the representation specification to be... for Wait_Value use record Wait at 1 range 5..7; Value at 0 range 6..12; Pad at 0 range 0..5; end record; (assuming System.Low_Order_First bit order) which works as expected. So - I can probably code up a portable version based on... - byte swap or not based on System.Default_Bit_Order - representation specification also based on System.Default_Bit_Order but is this the best way to do it or should I stick w/ the original code? Thanks. --Mark Johnson