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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2a0aa2b1c348fd6a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!usenet-fr.net!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: shifting bits Date: 29 Aug 2004 09:53:37 -0400 Organization: Cuivre, Argent, Or Message-ID: References: NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: melchior.cuivre.fr.eu.org 1093787633 72051 212.85.156.195 (29 Aug 2004 13:53:53 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Sun, 29 Aug 2004 13:53:53 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:3137 Date: 2004-08-29T09:53:37-04:00 fmdf@tiscali.it (fabio de francesco) writes: > I had to pack/unpack some variables into/from one. In order to get the > result I chose to shift the variables' bits using multiplications and > divisions by powers of two. You should also consider using a record with a representation clause; that will be much more readable. Something like: with Ada.Text_IO; use Ada.Text_IO; procedure Packed_Date is type Unsigned_4 is mod 2**4; type Unsigned_11 is mod 2**11; type Packed_Date_Type is record Year : Unsigned_11; Month : Unsigned_4; Day : Unsigned_4; end record; for Packed_Date_Type use record Year at 0 range 9 .. 19; Month at 0 range 5 .. 8; Day at 0 range 0 .. 4; end record; for Packed_Date_Type'Size use 20; Date : constant Packed_Date_Type := (Year => 1900, Month => 1, Day => 2); type Double_Packed_Date_Type is record Date_1 : Packed_Date_Type; Date_2 : Packed_Date_Type; end record; Double_Date : constant Double_Packed_Date_Type := (Date, Date); type Packed_Double_Packed_Date_Type is record Date_1 : Packed_Date_Type; Date_2 : Packed_Date_Type; end record; pragma Pack (Packed_Double_Packed_Date_Type); Packed_Double_Date : constant Packed_Double_Packed_Date_Type := (Date, Date); begin Put_Line ("Year => " & Unsigned_11'Image (Date.Year)); Put_Line ("Month => " & Unsigned_4'Image (Date.Month)); Put_Line ("Day => " & Unsigned_4'Image (Date.Day)); Put_Line ("Date'size => " & Integer'Image (Date'Size)); Put_Line ("Double_Date'size => " & Integer'Image (Double_Date'Size)); Put_Line ("Packed_Double_Date'size => " & Integer'Image (Packed_Double_Date'Size)); end Packed_Date; This compiles, and produces the output: ./packed_date.exe Year => 1900 Month => 1 Day => 2 Date'size => 24 Double_Date'size => 48 Packed_Double_Date'size => 40 I'm not sure I got the field sizes and locations correct; note how much clearer that is with this code than with your original. You do need to worry about byte endianness for this code; your shift and mask code is endian-independent. The object Date occupies 24 bits, because it must be an integral number of bytes. Packed_Double_Date is packed as expected. Note that the compiler will use shift and mask operations to access these fields. And it has a chance to do it more efficiently than if you code it, since it has more information to work with. -- -- Stephe