comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Bitwise XOR?
Date: Sat, 04 Oct 2003 02:57:38 GMT
Date: 2003-10-04T02:57:38+00:00	[thread overview]
Message-ID: <CGqfb.400517$2x.135450@rwcrnsc52.ops.asp.att.net> (raw)
In-Reply-To: mailman.28.1065177023.25614.comp.lang.ada@ada-france.org

The unchecked conversion must be used is to avoid the range check.  For
example, the following code causes a a range check exception:

declare
  a : Integer;
  b : Integer;
begin
  a := -1;
  b := 1;
  result := a xor b;
end;

Steve
(The Duck)

"Beard, Frank Randolph CIV" <frank.beard@navy.mil> wrote in message
news:mailman.28.1065177023.25614.comp.lang.ada@ada-france.org...
You don't even need the Unchecked_Conversion.

The following should work:

  function "xor"(Left,Right : in Integer) return Integer is
    pragma inline("xor");
    type Int_As_Mod is mod 2 ** Integer'size;
  begin
    return Integer( Int_As_Mod(Left) XOR Int_As_Mod(Right) );
  end "xor";

Theoretically, Unchecked_Conversion should be faster I guess.
Is that why you chose it, or did you do some timing tests
that showed a noticeable difference?

Frank

-----Original Message-----
From: Steve

Ada 95 permits using XOR on modular types.
If you want to XOR between integers, convert the integer to a modular type,
do the XOR,
then convert the result back.  The following (tested) routine does exactly
this.

  function "xor"(Left,Right : in Integer) return Integer is
    pragma inline("xor");
    type Int_As_Mod is mod 2 ** Integer'size;
    function To_Mod is new Ada.Unchecked_Conversion( Integer, Int_As_Mod );
    function To_Int is new Ada.Unchecked_Conversion( Int_As_Mod, Integer );
  begin
    return To_Int( To_Mod(Left) XOR To_Mod(Right) );
  end "xor";

Steve
(The Duck)





  reply	other threads:[~2003-10-04  2:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-03 10:28 Bitwise XOR? Beard, Frank Randolph CIV
2003-10-04  2:57 ` Steve [this message]
2003-10-04  3:07 ` Robert I. Eachus
  -- strict thread matches above, loose matches on Subject: below --
2003-10-02  1:35 David N. Maez
2003-10-02  2:03 ` sk
2003-10-02  9:01   ` John McCabe
2003-10-02  8:30 ` Adrian Knoth
2003-10-02 12:46   ` Marin David Condic
2003-10-02 12:53     ` Adrian Knoth
2003-10-02 18:15     ` Jeffrey Carter
2003-10-03 12:26       ` Marin David Condic
2003-10-03 20:32         ` Jeffrey Carter
2003-10-03 22:41           ` Marin David Condic
2003-10-03  3:14   ` Steve
replies disabled

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