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,ca7e14ee1b312f90 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-04-25 11:51:22 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "David C. Hoos" Newsgroups: comp.lang.ada Subject: Re: Minimum Record Size? LONG Date: Fri, 25 Apr 2003 13:51:20 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Complaints-To: abuse@supernews.com Xref: archiver1.google.com comp.lang.ada:36568 Date: 2003-04-25T13:51:20-05:00 List-Id: ----- Original Message ----- From: "Dr Nancy's Sweetie" Newsgroups: comp.lang.ada To: Sent: Friday, April 25, 2003 11:33 AM Subject: Minimum Record Size? LONG > > I'm working on a program which has to take four six-bit hunks of data > and regroup them into three eight-bit hunks. My first pass looked like > this: Here is an approach that, while verbose, is written to be understandable by the reader. The reason for the difference between your expected and actual results is the failure to take into account the machine endianness. Obviously, you are doing this on a little-endian machine. This is taken care of by the conditionally-called Reversed_Bits function. -- Begin Ada source code with Ada.Text_IO; with Ada.Unchecked_Conversion; with System; procedure Kilroy is type Eight is mod 2 ** 8; type Three_Eights is array (1 .. 3) of Eight; for Three_Eights'Size use 24; type Six is mod 2 ** 6; type Four_Sixes is array (1 .. 4) of Six; function Four_Sixes_To_Three_Eights (X : Four_Sixes) return Three_Eights is type Bit is mod 2; for Bit'Size use 1; type Bit_Array is array (Positive range <>) of Bit; pragma Pack (Bit_Array); subtype Twenty_Four_Bits is Bit_Array (1 .. 24); subtype Six_Bits is Bit_Array (1 .. 6); subtype Eight_Bits is Bit_Array (1 .. 8); function To_Bits is new Ada.Unchecked_Conversion (Source => Six, Target => Six_Bits); function To_Bits is new Ada.Unchecked_Conversion (Source => Eight, Target => Eight_Bits); function From_Bits is new Ada.Unchecked_Conversion (Source => Six_Bits, Target => Six); function From_Bits is new Ada.Unchecked_Conversion (Source => Eight_Bits, Target => Eight); function From_Bits is new Ada.Unchecked_Conversion (Source => Twenty_Four_Bits, Target => Three_Eights); function Reversed_Bits (X : Bit_Array) return Bit_Array is Result : Bit_Array (1 .. X'Length); begin for I in Result'Range loop Result (I) := X (X'Length - I + 1); end loop; return Result; end Reversed_Bits; Input : Four_Sixes := X; Temp : Twenty_Four_Bits; Result : Three_Eights; use type System.Bit_Order; begin if System.Default_Bit_Order = System.Low_Order_First then for I in Input'Range loop Input (I) := From_Bits (Reversed_Bits (To_Bits (Input (I)))); end loop; end if; for I in Input'Range loop Temp (6 * (I - 1) + 1 .. 6 * I) := To_Bits (Input (I)); end loop; Result := From_Bits (Temp); if System.Default_Bit_Order = System.Low_Order_First then for I in Result'Range loop Result (I) := From_Bits (Reversed_Bits (To_Bits (Result (I)))); end loop; end if; return Result; end Four_Sixes_To_Three_Eights; package Six_IO is new Ada.Text_IO.Modular_IO (Six); package Eight_IO is new Ada.Text_IO.Modular_IO (Eight); Input : constant Four_Sixes := (28, 55, 25, 50); Result : Three_Eights; begin Result := Four_Sixes_To_Three_Eights (Input); Ada.Text_IO.Put ("Input : "); for I in Input'Range loop Six_IO.Put (Item => Input (I)); end loop; Ada.Text_IO.New_Line; Ada.Text_IO.Put ("Result: "); for I in Result'Range loop Eight_IO.Put (Item => Result (I)); end loop; Ada.Text_IO.New_Line; end Kilroy; -- End Ada source code I hope this helps. David