comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: generic function and overloading
Date: Thu, 18 Oct 2007 17:23:24 -0400
Date: 2007-10-18T17:23:24-04:00	[thread overview]
Message-ID: <wcck5pk86cz.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: m2bqaw6uor.fsf@mac.com

Simon Wright <simon.j.wright@mac.com> writes:

> Robert A Duff <bobduff@shell01.TheWorld.com> writes:
>
>> But do you really want to do arithmetic on these types,
>> as well as setting/clearing bits?  If all you want to
>> do is set/clear bits, you might want to consider using
>> a packed array of Boolean.  Then to set the Nth bit you
>> can say:
>>
>>     X (N) := True;
>>
>> Alternatively, a packed array of type Bit, where:
>>
>>     type Bit is range 0..1;
>>
>> If the individual bit numbers have some specific meaning,
>> the index type could be an enumeration type.
>
> Will give surprising results on a big-endian machine.

Only if you're doing something that exposes endianness.
Endianness should be completely invisible to most code.
(E.g., if you set the N'th bit of an array of Boolean,
and get the N'th bit, you get back what you set.
The same is true with masking modular integers.)

>... At least code
> using shifts and masks is portable if gruesome

Well, it can be portable, but you can still get into endianness
trouble if you do things like splitting the integer into
individual bytes (e.g. to transmit them across a network).

>... -- and no more gruesome
> than code I've written which says
>
>    type Unrepresented is ..
>    Internal : Unrepresented;
>    ...
>
>    if System.Default_Bit_Order = System.High_Order_First then
>       declare
>          type Represented is ..
>          for Represented use ..
>             -- record rep with big-endian usage
>          External : constant Represented := Internal;

Do you mean "type Represented is new Unrepresented;"?
Then you need a type conversion in the line above.

Nitpick: I'd use a case statement rather than an if statement, here.

>       begin
>          Output (External);
>       end;
>    else
>       --  equivalent for l-e

- Bob



  reply	other threads:[~2007-10-18 21:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-18  7:28 generic function and overloading eliben
2007-10-18  8:18 ` Jean-Pierre Rosen
2007-10-18  9:31 ` Dmitry A. Kazakov
2007-10-18  9:33   ` Dmitry A. Kazakov
2007-10-18 10:32 ` Georg Bauhaus
2007-10-18 11:07 ` Stephen Leake
2007-10-18 11:30 ` Stefan Lucks
2007-10-18 15:58 ` Robert A Duff
2007-10-18 20:20   ` Simon Wright
2007-10-18 21:23     ` Robert A Duff [this message]
2007-10-19  5:19       ` Simon Wright
2007-10-19 13:59         ` Robert A Duff
2007-10-18 18:58 ` Dr. Adrian Wrigley
2007-10-18 22:30   ` Adam Beneschan
replies disabled

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