* 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
* Re: Bitwise XOR?
2003-10-02 1:35 Bitwise XOR? David N. Maez
@ 2003-10-02 2:03 ` sk
2003-10-02 9:01 ` John McCabe
2003-10-02 8:30 ` Adrian Knoth
1 sibling, 1 reply; 14+ messages in thread
From: sk @ 2003-10-02 2:03 UTC (permalink / raw)
To: comp.lang.ada
"David N. Maez" <sellout@dharmadevil.com>:
> ...
Try using modular types.
with Ada.Text_IO; use Ada.Text_IO;
procedure foobar is
type Unsigned_32 is mod 2 ** 32;
x,y : Unsigned_32;
begin
x := 12;
y := 23;
x := x xor y;
put_line(x'img);
end foobar;
--
-------------------------------------------------
-- Merge vertically for real address
--
-- s n p @ t . o
-- k i e k c c m
-------------------------------------------------
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
2003-10-02 1:35 Bitwise XOR? David N. Maez
2003-10-02 2:03 ` sk
@ 2003-10-02 8:30 ` Adrian Knoth
2003-10-02 12:46 ` Marin David Condic
2003-10-03 3:14 ` Steve
1 sibling, 2 replies; 14+ messages in thread
From: Adrian Knoth @ 2003-10-02 8:30 UTC (permalink / raw)
David N. Maez <sellout@dharmadevil.com> wrote:
> x : integer;
> y : integer;
> x := x xor y;
> What am I missing?
There is no XOR defined for anything else than bits. You'll need
to mask the bits and recombine the result.
I did it some time ago, I'm not sure whether it is right, perhaps
it should be done on mod-datatypes.
function "xor" (Left, Right : in Integer) return Integer is
result : Integer := 0;
leftrest : Integer := Left;
rightrest : Integer := Right;
boolleft, boolright : Boolean;
begin
for I in 0 .. Integer'Size - 1 loop
if (leftrest mod 2 = 1) then
boolleft := True;
else
boolleft := False;
end if;
leftrest := leftrest / 2;
if (rightrest mod 2 = 1) then
boolright := True;
else
boolright := False;
end if;
rightrest := rightrest / 2;
if (boolleft xor boolright) then
result := result + 2**(I);
end if;
end loop;
return result;
end "xor";
--
mail: adi@thur.de http://adi.thur.de PGP: v2-key via keyserver
M�nner sind wie Jeans: blau und steif...
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
2003-10-02 2:03 ` sk
@ 2003-10-02 9:01 ` John McCabe
0 siblings, 0 replies; 14+ messages in thread
From: John McCabe @ 2003-10-02 9:01 UTC (permalink / raw)
On Wed, 01 Oct 2003 21:03:37 -0500, sk <noname@myob.com> wrote:
>"David N. Maez" <sellout@dharmadevil.com>:
> > ...
>
>Try using modular types.
>
>with Ada.Text_IO; use Ada.Text_IO;
>procedure foobar is
>
> type Unsigned_32 is mod 2 ** 32;
>
> x,y : Unsigned_32;
>
>begin
> x := 12;
> y := 23;
> x := x xor y;
> put_line(x'img);
>end foobar;
For the record....
http://www.adaic.org/standards/95lrm/html/RM-4-5-1.html#I2480
Best Regards
John McCabe
To reply by email replace 'nospam' with 'assen'
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
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 3:14 ` Steve
1 sibling, 2 replies; 14+ messages in thread
From: Marin David Condic @ 2003-10-02 12:46 UTC (permalink / raw)
In case you didn't notice the related posts, yes there are logical
operators for things other than Booleans. You can do them on modular
types. You can also do them on packed arrays of Booleans. Check the ARM
- its pretty useful stuff if you're into bit-twiddling.
MDC
Adrian Knoth wrote:
>
> There is no XOR defined for anything else than bits. You'll need
> to mask the bits and recombine the result.
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jsf.mil/NSFrames.htm
Send Replies To: m c o n d i c @ a c m . o r g
"All reformers, however strict their social conscience,
live in houses just as big as they can pay for."
--Logan Pearsall Smith
======================================================================
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
2003-10-02 12:46 ` Marin David Condic
@ 2003-10-02 12:53 ` Adrian Knoth
2003-10-02 18:15 ` Jeffrey Carter
1 sibling, 0 replies; 14+ messages in thread
From: Adrian Knoth @ 2003-10-02 12:53 UTC (permalink / raw)
Marin David Condic <nobody@noplace.com> wrote:
> In case you didn't notice the related posts, yes there are logical
> operators for things other than Booleans. You can do them on modular
> types.
Thanks for pointer, I didn't know that.
--
mail: adi@thur.de http://adi.thur.de PGP: v2-key via keyserver
Lieber Gott, schmei� Hirn auf Dein verlorenes Schaf Johannes Sackmann. Und
wickel es vorher in Stahl ein. (Robin S. Socha in de.comp.security.misc)
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
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
1 sibling, 1 reply; 14+ messages in thread
From: Jeffrey Carter @ 2003-10-02 18:15 UTC (permalink / raw)
Marin David Condic wrote:
> In case you didn't notice the related posts, yes there are logical
> operators for things other than Booleans. You can do them on modular
> types. You can also do them on packed arrays of Booleans. Check the ARM
> - its pretty useful stuff if you're into bit-twiddling.
Logical operations are defined on one-dimensional arrays with a Boolean
component type, packed or not.
--
Jeff Carter
"You cheesy lot of second-hand electric donkey-bottom biters."
Monty Python & the Holy Grail
14
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
2003-10-02 8:30 ` Adrian Knoth
2003-10-02 12:46 ` Marin David Condic
@ 2003-10-03 3:14 ` Steve
1 sibling, 0 replies; 14+ messages in thread
From: Steve @ 2003-10-03 3:14 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1125 bytes --]
"Adrian Knoth" <adi@thur.de> wrote in message
news:slrnbnnohq.26h.adi@ppc201.mipool.uni-jena.de...
> David N. Maez <sellout@dharmadevil.com> wrote:
>
> > x : integer;
> > y : integer;
> > x := x xor y;
>
> > What am I missing?
>
> There is no XOR defined for anything else than bits. You'll need
> to mask the bits and recombine the result.
>
Not true.
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)
[excessively complicated untested example deleted]
> --
> mail: adi@thur.de http://adi.thur.de PGP: v2-key via keyserver
>
> M�nner sind wie Jeans: blau und steif...
^ permalink raw reply [flat|nested] 14+ messages in thread
* 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
* Re: Bitwise XOR?
2003-10-02 18:15 ` Jeffrey Carter
@ 2003-10-03 12:26 ` Marin David Condic
2003-10-03 20:32 ` Jeffrey Carter
0 siblings, 1 reply; 14+ messages in thread
From: Marin David Condic @ 2003-10-03 12:26 UTC (permalink / raw)
Note that *technically* speaking, I'm not "wrong" - I only observed that
you *could* do the operations on packed arrays of boolean. I didn't say
you *couldn't* do them on unpacked arrays. :-) Having soothed my ego,
thanks for the correction. ;-)
MDC
Jeffrey Carter wrote:
>
> Logical operations are defined on one-dimensional arrays with a Boolean
> component type, packed or not.
>
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jsf.mil/NSFrames.htm
Send Replies To: m c o n d i c @ a c m . o r g
"All reformers, however strict their social conscience,
live in houses just as big as they can pay for."
--Logan Pearsall Smith
======================================================================
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
2003-10-03 12:26 ` Marin David Condic
@ 2003-10-03 20:32 ` Jeffrey Carter
2003-10-03 22:41 ` Marin David Condic
0 siblings, 1 reply; 14+ messages in thread
From: Jeffrey Carter @ 2003-10-03 20:32 UTC (permalink / raw)
Marin David Condic wrote:
> Note that *technically* speaking, I'm not "wrong" - I only observed that
> you *could* do the operations on packed arrays of boolean. I didn't say
> you *couldn't* do them on unpacked arrays. :-) Having soothed my ego,
> thanks for the correction. ;-)
>
> MDC
>
> Jeffrey Carter wrote:
>
>> Logical operations are defined on one-dimensional arrays with a
>> Boolean component type, packed or not.
Now that your ego is soothed, should I point out that a Boolean type is
type Boolean or any type derived from it?
--
Jeff Carter
"This school was here before you came,
and it'll be here before you go."
Horse Feathers
48
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Bitwise XOR?
2003-10-03 20:32 ` Jeffrey Carter
@ 2003-10-03 22:41 ` Marin David Condic
0 siblings, 0 replies; 14+ messages in thread
From: Marin David Condic @ 2003-10-03 22:41 UTC (permalink / raw)
Oh go ahead. Have a ball. :-)
MDC
Jeffrey Carter wrote:
>
> Now that your ego is soothed, should I point out that a Boolean type is
> type Boolean or any type derived from it?
>
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jsf.mil/NSFrames.htm
Send Replies To: m c o n d i c @ a c m . o r g
"All reformers, however strict their social conscience,
live in houses just as big as they can pay for."
--Logan Pearsall Smith
======================================================================
^ permalink raw reply [flat|nested] 14+ messages in thread
* 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
1 sibling, 0 replies; 14+ messages in thread
From: Steve @ 2003-10-04 2:57 UTC (permalink / raw)
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)
^ permalink raw reply [flat|nested] 14+ messages in thread
* 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
1 sibling, 0 replies; 14+ messages in thread
From: Robert I. Eachus @ 2003-10-04 3:07 UTC (permalink / raw)
Beard, Frank Randolph CIV wrote:
> 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?
First, the Unchecked_Conversion is correct. The above code will not do
what you expect with negative numbers, it will raise an exception. The
Unchecked_Conversion will not only prevent the "unnecessary" check, but
will also be faster.
--
Robert I. Eachus
"Quality is the Buddha. Quality is scientific reality. Quality is the
goal of Art. It remains to work these concepts into a practical,
down-to-earth context, and for this there is nothing more practical or
down-to-earth than what I have been talking about all along...the repair
of an old motorcycle." -- from Zen and the Art of Motorcycle
Maintenance by Robert Pirsig
^ 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-02 1:35 Bitwise XOR? 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
-- strict thread matches above, loose matches on Subject: below --
2003-10-03 10:28 Beard, Frank Randolph CIV
2003-10-04 2:57 ` Steve
2003-10-04 3:07 ` Robert I. Eachus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox