comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@acm.org>
To: comp.lang.ada@ada-france.org
Subject: Re: shifting bits
Date: 29 Aug 2004 09:53:37 -0400
Date: 2004-08-29T09:53:37-04:00	[thread overview]
Message-ID: <mailman.18.1093787632.31213.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <ba2f9c57.0408281738.61a48643@posting.google.com>

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




  parent reply	other threads:[~2004-08-29 13:53 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-29  1:38 shifting bits fabio de francesco
2004-08-29  3:51 ` Jim Rogers
2004-08-29  6:51   ` Martin Krischik
2004-08-29 12:53   ` fabio de francesco
2004-08-29 13:21     ` Ludovic Brenta
2004-08-29 17:58       ` Simon Wright
2004-08-29 14:25     ` Michael Bode
2004-08-29 11:29 ` Ludovic Brenta
2004-08-29 17:17   ` tmoran
2004-08-29 13:53 ` Stephen Leake [this message]
2004-08-29 17:17 ` tmoran
2004-08-30  3:49   ` fabio de francesco
2004-08-30  4:16     ` tmoran
2004-08-31  0:22       ` fabio de francesco
2004-10-10 17:51     ` ACM Queue Ada article tmoran
2004-10-11 13:13       ` Marc A. Criley
2004-10-12 18:32         ` skidmarks
2004-11-30 18:04     ` SIGAda ad tmoran
2005-05-25  7:19     ` Gnat STORAGE_ERROR tmoran
2005-05-25  8:53       ` Alex R. Mosteo
2005-05-25  9:51         ` Duncan Sands
2005-05-25 10:05           ` Alex R. Mosteo
2005-05-25 11:32             ` Duncan Sands
2005-05-25 12:16               ` Alex R. Mosteo
2005-05-26  9:39                 ` Duncan Sands
2005-05-26  9:53                   ` Alex R. Mosteo
2005-05-26 19:32                   ` Björn Persson
2005-05-25 18:30         ` tmoran
2005-05-26  2:19           ` David C. Hoos, Sr.
2005-05-26 18:42             ` tmoran
2005-05-27  7:19               ` workaround, was " tmoran
2005-07-29 21:08     ` shifting bits tmoran
2005-07-30 20:28     ` Ada utility for Google Earth, NASA World Wind tmoran
2005-08-08 13:27       ` Marc A. Criley
2005-08-08 21:08         ` tmoran
2005-08-10  8:11         ` Rob Norris
2005-08-10  8:35           ` tmoran
2005-08-10 11:41             ` Rob Norris
2005-08-10 18:05               ` tmoran
2004-08-30  9:03 ` shifting bits Ole-Hjalmar Kristensen
2004-08-30 11:37   ` Dale Stanbrough
2004-08-30 18:38     ` Jeffrey Carter
2004-08-30 15:19 ` Martin Krischik
2004-08-30 17:17   ` Georg Bauhaus
2004-08-30 18:48     ` Jeffrey Carter
2004-08-31 12:37       ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox