comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic.brenta@insalien.org>
Subject: Re: Bitmanipulation in Ada
Date: Wed, 18 Aug 2004 22:51:45 +0200
Date: 2004-08-18T22:52:21+02:00	[thread overview]
Message-ID: <87k6vwrwym.fsf@insalien.org> (raw)
In-Reply-To: Xns9549E5E9CFFB1BerndSpechgmxcom@151.189.20.10

 (Bernd Specht) writes:
> Hi, 
>
> i have some questions regarding bitmanipulations:
>
> 1. I found bitwise "and", "or" and "xor" operations, but as far as I 
> understand they are applicable only for modular types or packed boolean 
> arrays. Is this correct?

Yes.

> 2. I did not found shift and rotate operations. Did I miss something or are 
> there really none?

These operations are usually carried out in hardware for certain sizes
of modular integers only.  Consequently, these operations are defined
in package Interfaces (see RM 3.5.4(31) and B.2).  Package Interfaces
defines the following subprograms:

   function Shift_Left  (Value  : Unsigned_n;
                         Amount : Natural) return Unsigned_n;
   function Shift_Right (Value  : Unsigned_n;
                         Amount : Natural) return Unsigned_n;
   function Shift_Right_Arithmetic (Value  : Unsigned_n;
                                    Amount : Natural)
     return Unsigned_n;
   function Rotate_Left  (Value  : Unsigned_n;
                          Amount : Natural) return Unsigned_n;
   function Rotate_Right (Value  : Unsigned_n;
                          Amount : Natural) return Unsigned_n;

Each compiler provides Unsigned_n types for various values of n
corresponding to natural sizes of the target hardware.

Doing bit rotations in the general case does not necessarily make
sense, because the modulo of the type may not be a power of two.

> 3. When I want treat a value both as an integer and as a boolean array, how 
> can i do this? In pascal I would use a tagless record like 
>
> TYPE ov is record
>   case boolean of
>     true : i : integer;
>     false  : byte_array;
>   end;
>   end;
>
> Such overlay structures are not valid with Ada, so what do instead?

Have you considered a variant record?

type Ov (T : Boolean) is record
   case T is
      when True => I : Integer;
      when False => B : Byte_Array;
   end T;
end record;

If what you really want is to convert an integer to a byte array, use
Unchecked_Conversion:

with Ada.Unchecked_Conversion;
procedure P is
   type Byte_Array is array (1 .. Integer'Size) of Boolean;
   pragma Pack (Byte_Array);
   for Byte_Array'Size use Integer'Size;
   function To_Byte_Array is new
      Ada.Unchecked_Conversion (Source => Integer, Target => Byte_Array);
   I : Integer := 42;
   B : Byte_Array := To_Byte_Array (I);
begin
   null;
end P;


-- 
Ludovic Brenta.




  reply	other threads:[~2004-08-18 20:51 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 [this message]
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
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