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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,705f377a5e04a446 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!feeder6.cambrium.nl!feeder5.cambrium.nl!feed.tweaknews.nl!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Bit operations in Ada References: Date: Sat, 24 May 2008 00:08:11 +0200 Message-ID: <87wslksmgk.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:UYAmm+3rPGr8yawa+S173sOVTs8= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tele2 X-Trace: DXC=WWQY?MLDK;Zk==n`0V^Y5[6`Y6aWje^YZi;H6C@SF_2_@WAS81_gI6V3;B5mk`UjL]c;9J[jF59R] Xref: g2news1.google.com comp.lang.ada:304 Date: 2008-05-24T00:08:11+02:00 List-Id: Dennis Hoppe writes: > Hello, > > I'm new to Ada and bitwise operations is a new challenge in this > realm. My objective is to manipulate some bit strings in Ada, > especially: > > a) addition/subtraction mod 2**n, > b) change bits directly (e.g, via array access) > c) shift operations > d) rotate operations > e) and, xor, not, or > > I started with an array of booleans of size 2**n, that provides neat > access to individual bits by means of an index. Unfortunately, > addition/subtraction mod 2**n is not supported, but essential for me. > > Next, I tried modular types (mod 2**n), but ended up with not having > direct access to individual bits. My last attempt was to use > Interfaces.Unsinged_n. It is a solid package that simplifies the usage > of bitwise operations for me by adding shift and rotate > operations. Addition modulo n works well, but I have to dispense with > direct bit access, too. > > So, what is coming next? Should I go with Interfaces.Unsinged_n and > provide a suitable function that converts the used type into an array > of boolean? Maybe, I'm ought to use directly an array of boolean and > try to convert the array into an Unsigned_n; if required to add two > bit-strings. IIUC, modular types such as Interfaces.Unsigned_n provide all operations you need except for direct bit access. Solution 1: You can access bits using "and", "or", "not", "**" and "=" like so: declare A : Interfaces.Unsigned_32 := 2#00000000000000000000000000000000#; use type Interfaces.Unsigned_32; Is_Bit_8_Set : Boolean; begin A := A or 2 ** 5; -- Set bit 5 to 1 Is_Bit_8_Set := A and 2 ** 8 /= 0; -- read bit 8 end; Solution 2: If this is too difficult or error-prone, you could wrap that into inlined subprograms like so: type Bit_Number is range 0 .. 31; procedure Set (Bit : in Bit_Number; In_Value : in out Interfaces.Unsigned_32; To : in Boolean) is use type Interfaces.Unsigned_32; begin if To = True then In_Value := In_Value or 2 ** Bit; else In_Value := In_Value and not 2 ** Bit; end if; end Set; function Is_Set (Bit : in Bit_Number; In_Value : in Interfaces.Unsigned_32) return Boolean is use type Interfaces.Unsigned_32; begin return In_Value and 2 ** Bit /= 0; end Is_Set; Solution 3: Another alternative is Unchecked_Conversion: type Bit_Number is range 0 .. 31; type Bit_Field is array (Bit_Number) of Boolean; pragma Pack (Bit_Field); function To_Bit_Field is new Ada.Unchecked_Conversion (Source => Interfaces.Unsigned_32, Target => Bit_Field); function To_Unsigned_32 is new Ada.Unchecked_Conversion (Source => Bit_Field, Target => Interfaces.Unsigned_32); procedure Set (Bit : in Bit_Number; In_Value : in out Interfaces.Unsigned_32; To : in Boolean) is Field : Bit_Field := To_Bit_Field (In_Value); begin Field (Bit) := To; In_Value := To_Unsigned_32 (Field); end Set; HTH -- Ludovic Brenta.