comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Byte/Bit Twiddling in Ada
Date: 1997/02/14
Date: 1997-02-14T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001402972052280001@news.ni.net> (raw)
In-Reply-To: 5dvfnn$olj@neocad.xilinx.com


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
<mailto:matthew_heaney@acm.org>
(818) 985-1271




       reply	other threads:[~1997-02-14  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <5dvfnn$olj@neocad.xilinx.com>
1997-02-14  0:00 ` Matthew Heaney [this message]
1997-02-14  0:00 ` Byte/Bit Twiddling in Ada Mats Weber
1997-02-15  0:00   ` Robert Dewar
1997-02-15  0:00   ` Robert Dewar
1997-02-15  0:00     ` wiljan
1997-02-26  0:00       ` Robert Dewar
1997-02-14  0:00 ` Keith Allan Shillington
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox