From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Bit operations in Ada (endianness)
Date: Mon, 02 Jun 2008 19:38:15 +0200
Date: 2008-06-02T19:38:15+02:00 [thread overview]
Message-ID: <877id7ka9k.fsf@ludovic-brenta.org> (raw)
In-Reply-To: g20sg4$nkb$1@aioe.org
Dennis Hoppe <dennis.hoppe@hoppinet.de> writes:
> Hi,
>
> I have another question regarding litte and big-endian. Is there
> any way to declare, that the type
>
> type Bit_Field is array (Bit_Number) of Boolean;
>
> is represented by litte or big-endian? Bit_Number is a generic
> paremeter (mod <>). I figured out to set the endianness to
> record types, but it does not work for the above array type.
The arithmetic on modular types does what you need, so the problem
only arises when using the bit-array representation. You need to
invert the index explicitly on little-endian machines.
> I also wonder, if I can query the operating system via Ada,
> which endianness it uses.
Look at System.Default_Bit_Order.
So a solution might look like:
generic
type Bit_Number is mod <>;
package Bit_Fields is
type Bit_Field is array (Bit_Number) of Boolean;
function Index (N : Bit_Number) return Bit_Number;
end Bit_Numbers;
with System;
package body Bit_Numbers is
function Index (N : Bit_Number) return Bit_Number is
begin
case System.Default_Bit_Order is
when System.High_Order_First => return N;
when System.Low_Order_First => return Bit_Number'Last - N;
end case;
end Index;
end Bit_Numbers;
HTH
--
Ludovic Brenta.
next prev parent reply other threads:[~2008-06-02 17:38 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-23 21:19 Bit operations in Ada Dennis Hoppe
2008-05-23 22:08 ` Ludovic Brenta
2008-05-24 15:36 ` Simon Wright
2008-06-02 13:27 ` Bit operations in Ada (endianness) Dennis Hoppe
2008-06-02 14:01 ` (see below)
2008-06-02 18:22 ` Jeffrey R. Carter
2008-06-02 17:38 ` Ludovic Brenta [this message]
2008-05-23 22:38 ` Bit operations in Ada Robert A Duff
2008-05-24 0:27 ` Randy Brukardt
2008-05-24 9:40 ` Bit operations in Ada (Thank you) Dennis Hoppe
2008-05-23 23:25 ` Bit operations in Ada Jeffrey R. Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox