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,53a810f13e8ba40 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Byte/Bit Twiddling in Ada Date: 1997/02/14 Message-ID: #1/1 X-Deja-AN: 218920054 References: <5dvfnn$olj@neocad.xilinx.com> Content-Type: text/plain; charset=ISO-8859-1 Organization: Estormza Software Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1997-02-14T00:00:00+00:00 List-Id: In article <5dvfnn$olj@neocad.xilinx.com>, kruse@xilinx.com (Jim Kruse) wrote: >Is there any portable way to do byte and bit manipulation in Ada, on a >long word? I found no mention of this topic in the FAQ. Yes. It may be that using Unchecked_Conversion is called for. On the other hand, if you're on a UNIX box you may be able to bind to htons, for example. > >My friend is trying to write some code that does byte swapping, >presumably to handle conversions to and from IEEE number formats. Well, here's an example of byte-swapping a longword (32 bits) that uses Unchecked_Conversion. Note also that bit manipulation is fully supported in Ada; see the package Interfaces. type Byte is new Integer_8; type Byte_Array is array (Positive range <>) of Byte; subtype Longword_As_Byte_Array is Byte_Array (1 .. 4); function To_Byte_Array is new Unchecked_Conversion ( Source => Integer_32, Target => Longword_As_Byte_Array); function To_Longword is new Unchecked_Conversion ( Source => Longword_As_Byte_Array, Target => Integer_32); function Byte_Swapped (O : Integer_32) return Integer_32 is The_Byte_Array : Longword_As_Byte_Array := To_Byte_Array (O); begin The_Byte_Array := The_Byte_Array (4) & The_Byte_Array (3) & The_Byte_Array (2) & The_Byte_Array (1); return To_Longword (The_Byte_Array); end; -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271