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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8309f2bc055237c4 X-Google-Attributes: gid103376,public From: lutz@iks-jena.de (Lutz Donnerhacke) Subject: Re: Bit manipulation Date: 2000/11/13 Message-ID: #1/1 X-Deja-AN: 693030858 Distribution: world Content-Transfer-Encoding: 8bit References: <8u8v6n$b7o$1@nnrp1.deja.com> <2WTH$pdrCfOd@eisner.decus.org> <8ub6kt$6nd$1@nnrp1.deja.com> <8ubeq8$cgm$1@nnrp1.deja.com> <3A0D38E9.BB87D8CD@mindspring.com> Content-Type: text/plain; charset=ISO-8859-1 Organization: IKS GmbH Jena Mime-Version: 1.0 User-Agent: slrn/0.9.5.7 (UNIX) Newsgroups: comp.lang.ada Date: 2000-11-13T00:00:00+00:00 List-Id: * Redryder wrote: >I need to swap the bits in a 32-bit word. type word32 is 0 .. 2**32-1; procedure swap32 (i : in word32) returns word32 is type lowrec is record data : word32; end record; type highrec is record data : word32; end record; for lowrec'Bit_Order use Low_Order_First; for highrec'Bit_Order use High_Order_First; function l2h is new Ada.Unchecked_Conversion (lowrec, highrec); low : lowrec := (i); high : highrec := l2h(low); begin return high; end swap32; It's up to your compiler to find out what's a effictive way to implement this. Some architectures may use the endianess swap prefix opcode. Other question: GNAT 3.12p has problems with efficent code of a simple rotation. How to solve it portable? with Ada.Unchecked_Conversion; procedure binary_rotation is wordsize : constant := 16; type word is mod 2**wordsize; type bitfield is array(1..wordsize) of boolean; pragma Pack(bitfield); -- the following clause will not work without pragma for bitfield'Size use wordsize; function w2b is new Ada.Unchecked_Conversion (word, bitfield); function b2w is new Ada.Unchecked_Conversion (bitfield, word); subtype offset is Natural range 1 .. wordsize-1; function rotate (b : bitfield; s : offset) return bitfield is begin return b(b'first .. b'last - s) & b(b'last - s + 1 .. b'last); end rotate; begin null; end binary_rotation;