comp.lang.ada
 help / color / mirror / Atom feed
* RE: Bitwise XOR?
@ 2003-10-03 10:28 Beard, Frank Randolph CIV
  2003-10-04  2:57 ` Steve
  2003-10-04  3:07 ` Robert I. Eachus
  0 siblings, 2 replies; 14+ messages in thread
From: Beard, Frank Randolph CIV @ 2003-10-03 10:28 UTC (permalink / raw)
  To: Steve, comp.lang.ada

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)



^ permalink raw reply	[flat|nested] 14+ messages in thread
* Bitwise XOR?
@ 2003-10-02  1:35 David N. Maez
  2003-10-02  2:03 ` sk
  2003-10-02  8:30 ` Adrian Knoth
  0 siblings, 2 replies; 14+ messages in thread
From: David N. Maez @ 2003-10-02  1:35 UTC (permalink / raw)


I'm new to Ada, and I'm wondering what the bitwise (not logical) Exclusive
Or function is?
Using Gnat, this fails to compile:

with Ada.Text_IO; use Ada.Text_IO;
procedure foobar is
  x : integer;
  y : integer;
begin
  x := 12;
  y := 23;
  x := x xor y;
  put_line(x'img);
end foobar;

Gnat complains:
foobar.adb:9:10: invalid operand types for operator "xor"
foobar.adb:9:10: left operand has type "Standard.integer"
foobar.adb:9:10: right operand has type "Standard.integer"

What am I missing?





^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2003-10-04  3:07 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-03 10:28 Bitwise XOR? Beard, Frank Randolph CIV
2003-10-04  2:57 ` Steve
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

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