[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

XOR operator?

Daniel Carrera

10/28/2003 8:36:00 PM

Hi all,

I'm trying to figure out what the boolean "xor" operator is in Ruby. I
thought it was "^^" but that doesn't work. I searched through PickAxe and
couldn't find a mention of xor.

Does Ruby have an xor?

Cheers,
--
Daniel Carrera | OpenPGP KeyID: 9AF77A88
PhD grad student. |
Mathematics Dept. | "To understand recursion, you must first
UMD, College Park | understand recursion".

9 Answers

Mark J. Reed

10/28/2003 8:45:00 PM

0

On Wed, Oct 29, 2003 at 05:36:01AM +0900, Daniel Carrera wrote:
> Hi all,
>
> I'm trying to figure out what the boolean "xor" operator is in Ruby. I
> thought it was "^^" but that doesn't work. I searched through PickAxe and
> couldn't find a mention of xor.
>
> Does Ruby have an xor?

The xor operator in Ruby is ^ (just one). It serves as both bitwise
and Boolean, depending on its arguments:

irb(main):001:0> 2 ^ 1
=> 3
irb(main):002:0> true ^ false
=> true
irb(main):003:0> true ^ true
=> false

-Mark

gabriele renzi

10/28/2003 8:49:00 PM

0

il Wed, 29 Oct 2003 05:36:01 +0900, Daniel Carrera
<dcarrera@math.umd.edu> ha scritto::

>Hi all,
>
>I'm trying to figure out what the boolean "xor" operator is in Ruby. I
>thought it was "^^" but that doesn't work. I searched through PickAxe and
>couldn't find a mention of xor.


it is just one ^
>> 0b0000^0b0111
=> 7


Lyle Johnson

10/28/2003 9:04:00 PM

0

Daniel Carrera wrote:

> I'm trying to figure out what the boolean "xor" operator is in Ruby. I
> thought it was "^^" but that doesn't work.

It's just a single "^" character, e.g.

six = 2 ^ 4

Hope this helps,

Lyle

Jim Freeze

10/28/2003 9:22:00 PM

0

On Wednesday, 29 October 2003 at 5:36:01 +0900, Daniel Carrera wrote:
> Hi all,
>
> I'm trying to figure out what the boolean "xor" operator is in Ruby. I
> thought it was "^^" but that doesn't work. I searched through PickAxe and
> couldn't find a mention of xor.
>
> Does Ruby have an xor?

irb(main):001:0> 1 ^ 0
=> 1
irb(main):002:0> 1 ^ 1
=> 0
irb(main):003:0> 0 ^ 0
=> 0
irb(main):004:0> 0 ^ 1
=> 1


--
Jim Freeze
----------
The Roman Rule
The one who says it cannot be done should never interrupt the
one who is doing it.

Dalibor Sramek

10/28/2003 9:22:00 PM

0

On Wed, Oct 29, 2003 at 05:36:01AM +0900, Daniel Carrera wrote:
> I'm trying to figure out what the boolean "xor" operator is in Ruby. I
> thought it was "^^" but that doesn't work. I searched through PickAxe and
> couldn't find a mention of xor.

Neither Ruby in a Nutshell mentions one. However I made a quick tests and it
seems it is safe to use ^ for integers.

Dalibor

--
Dalibor Sramek http://www.insu... | In the eyes of cats,
dalibor.sramek@insula.cz | all things belong to cats.

Daniel Carrera

10/28/2003 9:29:00 PM

0

Thanks everyone. Yeah, ^ seems to work.



On Tue, Oct 28, 2003 at 03:04:04PM -0600, Lyle Johnson wrote:
> Daniel Carrera wrote:
>
> >I'm trying to figure out what the boolean "xor" operator is in Ruby. I
> >thought it was "^^" but that doesn't work.
>
> It's just a single "^" character, e.g.
>
> six = 2 ^ 4
>
> Hope this helps,
>
> Lyle

--
Daniel Carrera | OpenPGP KeyID: 9AF77A88
PhD grad student. |
Mathematics Dept. | "To understand recursion, you must first
UMD, College Park | understand recursion".

Dave Thomas

10/28/2003 10:03:00 PM

0


On Oct 28, 2003, at 14:36, Daniel Carrera wrote:

> Hi all,
>
> I'm trying to figure out what the boolean "xor" operator is in Ruby. I
> thought it was "^^" but that doesn't work. I searched through PickAxe
> and
> couldn't find a mention of xor.
>

http://www.rubycentral.com/book/ref_c_falseclass.html#Fals...
http://www.rubycentral.com/book/ref_c_fixnum.html#Fixnum.Bit...

etc


Josef 'Jupp' Schugt

10/28/2003 11:22:00 PM

0

* Daniel Carrera; Wed, 29 Oct 2003 06:28:45 +0900

> Thanks everyone. Yeah, ^ seems to work.
[...as an XOR operator]

One should add that the exponentiation operator is '**'.
Matz, does that syntax mean that Ruby also has COBOL or FORTRAN
heritage (I am not aware of anything else that would justify that
assumption)?

Josef 'Jupp' Schugt

Mark J. Reed

10/29/2003 12:17:00 AM

0

On Tue, Oct 28, 2003 at 11:21:33PM +0000, Josef 'Jupp' Schugt wrote:
> One should add that the exponentiation operator is '**'.
> Matz, does that syntax mean that Ruby also has COBOL or FORTRAN
> heritage (I am not aware of anything else that would justify that
> assumption)?

AFAIK, Ruby got it from Perl, which got it from Fortran, only because
C has no exponentiation operator and ^ was taken by XOR.

-Mark