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.0 required=5.0 tests=BAYES_00,FILL_THIS_FORM_LOAN autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9dec3ff1604723d9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!transit.news.xs4all.nl!195.241.76.212.MISMATCH!tiscali!transit1.news.tiscali.nl!dreader2.news.tiscali.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Bitmanipulation in Ada References: From: Ludovic Brenta Date: Wed, 18 Aug 2004 22:51:45 +0200 Message-ID: <87k6vwrwym.fsf@insalien.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:DTMlvy/XyfLdgwzg4CKEjQlOGuo= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 18 Aug 2004 22:52:21 CEST NNTP-Posting-Host: 83.134.237.203 X-Trace: 1092862341 dreader2.news.tiscali.nl 62357 83.134.237.203:35656 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:2813 Date: 2004-08-18T22:52:21+02:00 List-Id: (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.