comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Bitmanipulation in Ada
Date: Thu, 19 Aug 2004 00:53:48 GMT
Date: 2004-08-19T00:53:48+00:00	[thread overview]
Message-ID: <wSSUc.27249$9Y6.977@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <Xns9549EB926B2CBBerndSpechgmxcom@151.189.20.10>

Bernd Specht wrote:
> Ok. Is there a reason why it is not (directly) available in the
> language itself like the and/or/xor or the >> and << in C?

Shift and rotate operations only make sense for types that are a
"natural" size for the underlying hardware. Those are the only kind of
integer types available in C. Since Ada also provides user-defined
numeric types, which may not be a natural size for the hardware, these
can only be provided for types which are a natural size.

> OK, but this would result in an assignment operation (a memory move
> on maschine code level). What I want is a real "overlay" (same
> storage location used for both), so reading the value would not need
> extra instructions.

That may be what you want. What a software engineer wants is the 
clearest software that meets the requirements. Unchecked_Conversion will 
almost always provide this.

In many cases, Unchecked_Conversion will generate no code, and need not 
involve an assignment. In well designed software, the part(s) of the 
software that deals with values as an integer and those that deal with 
them as bit arrays will be kept separate. So you do something like:

with Ada.Unchecked_Conversion;
procedure Deals_2_Ways is
    type Bit_Array is array (1 .. Integer'Size) of Boolean;
    pragma Pack (Bit_Array);
    for Bit_Array'Component_Size use 1;
    for Bit_Array'Size use Integer'Size;

    function Get return Integer is separate;

    Value : Integer := Get;

    procedure Deal_With_Integer (Value : in out Integer) is separate;
    procedure Deal_With_Bit_Array (Value : in out Bit_Array) is separate;

    function To_Bit is new Ada.Unchecked_Conversion
    (Source => Integer, Target => Bit_Array);
begin -- Deals_2_Ways
    Deal_With_Integer (Value => Value);
    Deal_With_Bit_Array (Value => To_Bit (Value) );
end Deals_2_Ways;

-- 
Jeff Carter
"I'm particularly glad that these lovely children were
here today to hear that speech. Not only was it authentic
frontier gibberish, it expressed a courage little seen
in this day and age."
Blazing Saddles
88




  parent reply	other threads:[~2004-08-19  0:53 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-18 20:37 Bitmanipulation in Ada Bernd Specht
2004-08-18 20:51 ` Ludovic Brenta
2004-08-18 21:10   ` Bernd Specht
2004-08-18 21:16     ` Ludovic Brenta
2004-08-18 21:18     ` Ed Falis
2004-08-19 17:30       ` Bernd Specht
2004-08-19 17:44         ` Ed Falis
2004-08-19  0:53     ` Jeffrey Carter [this message]
2004-08-19 17:44       ` Bernd Specht
2004-08-19 18:09         ` Martin Dowie
2004-08-19 18:28           ` Bernd Specht
2004-08-19 19:31             ` Martin Dowie
2004-08-19 20:29             ` Martin Dowie
2004-08-20 21:31               ` Bernd Specht
2004-08-19 19:17         ` Jeffrey Carter
2004-08-19 19:57           ` Björn Persson
2004-08-20  0:52             ` Jeffrey Carter
2004-08-19 21:24         ` Francois G. Dorais
2004-08-20  8:55           ` Pascal Obry
2004-08-20  7:26         ` Jean-Pierre Rosen
2004-08-20 21:20           ` Bernd Specht
2004-08-20 21:39             ` Ed Falis
2004-08-18 21:14 ` (see below)
2004-08-18 21:56   ` Martin Dowie
2004-08-19 15:25     ` (see below)
2004-08-19 15:50       ` Martin Dowie
2004-08-18 21:53 ` Martin Dowie
2004-08-18 22:59   ` Björn Persson
2004-08-19  8:08   ` Egil H. H�vik
2004-08-19 17:46   ` Bernd Specht
2004-08-20 20:57 ` Bitordering? was " Alfred Hilscher
2004-08-21 11:34   ` Nick Roberts
2004-08-21 14:00     ` Jim Rogers
2004-08-21 16:54       ` Simon Wright
2004-08-21 16:55     ` Georg Bauhaus
2004-08-23 18:36       ` Alfred Hilscher
2004-08-23 18:47     ` Alfred Hilscher
2004-08-23 22:39       ` Nick Roberts
replies disabled

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