^ operator

B

Berryl Hesh

I'm trying to translate some vb code to c#.

There is an if test on two bool variables like so:
If _mouseOver Xor isOver Then ..

I believe this translates to
if (_mouseOver ^ isOver) ..

Is this the equivalent of "if(_mouseOver || isOver)? Faster?

Thanks,
Berryl
 
M

Michael C

Berryl Hesh said:
I'm trying to translate some vb code to c#.

There is an if test on two bool variables like so:
If _mouseOver Xor isOver Then ..

I believe this translates to
if (_mouseOver ^ isOver) ..

Is this the equivalent of "if(_mouseOver || isOver)? Faster?

Xor is used by programmers who are trying to show off. Not equals is much
readable.

Michael
 
S

Stefan L

Hi Berryl,

yes, VB "Xor" translates to "^" in C#.
And no, it is not equivalent to "||", which is OrElse in VB.

These to operators have a a huge difference in evalutaion:
"||" will return true if either one of the oparands is True or both,
"^" will return True if only exactly one, but not both operands are True.

Consider this:
if (_mouseOver ^ isOver)
{
// do something once when the mouse enters/exits
SetMousePointerVisibility (_mouseOver);
isOver = _mouseOver;
}

This is typical code if you want to do something once a state has
changed (here: Hide a coursor on mouse enter and show on mouse exit).

Compare this to:
if (_mouseOver || isOver)
{ /* same code as above */ }

When using the program you might not see a difference. This is because
executing SetMousePointerVisibility() will always correctly set the
visibility of the mouse pointer. But it will always be called as long as
_mouseOver is true, not just once.

HTH,
Stefan
 
G

Göran Andersson

Berryl said:
I'm trying to translate some vb code to c#.

There is an if test on two bool variables like so:
If _mouseOver Xor isOver Then ..

I believe this translates to
if (_mouseOver ^ isOver) ..

Is this the equivalent of "if(_mouseOver || isOver)? Faster?

Thanks,
Berryl

Yes, ^ is the equivalent of Xor, but 'if' is not the exact equivalent of
'If'. The VB 'If' command accepts a numerical value as well as a boolean
value, while the C# 'if' command requires a boolean value.

What the VB code actually does is:

If (_mouseOver Xor isOver) <> 0 Then ...

Which in C# is:

if ((_mouseOver ^ isOver) != 0) ...

So, as Michael pointed out, this is just a different way of checking for
inequality. Intead of doing a numeric operation and compare the result
to zero, you should just compare the values:

If _mouseOver <> isOver Then ...

which translates to:

if (_mouseOver != isOver) ...
 
S

Stefan Hoffmann

hi Göran,

Göran Andersson said:
What the VB code actually does is:
If (_mouseOver Xor isOver) <> 0 Then ...
This is only true, when _mouseOver and isOver are declared as numerical
types (Int, Long). If they are declared as Boolean, then this expression
evaluates like this

(_mouseOver Xor isOver) <> 0
(_mouseOver Xor isOver) <> False
(_mouseOver Xor isOver) = True
(_mouseOver Xor isOver)

The problem is that Xor in VB(A) is both a boolean operation and a
bit-wise one.


mfG
--> stefan <--
 
G

Göran Andersson

Stefan said:
hi Göran,


This is only true, when _mouseOver and isOver are declared as numerical
types (Int, Long). If they are declared as Boolean, then this expression
evaluates like this

(_mouseOver Xor isOver) <> 0
(_mouseOver Xor isOver) <> False
(_mouseOver Xor isOver) = True
(_mouseOver Xor isOver)

The problem is that Xor in VB(A) is both a boolean operation and a
bit-wise one.


mfG
--> stefan <--

Yes, thanks for the correction. For boolean values the If translates
into an if without any further change:

If _mouseOver Xor isOver Then ...

translates exactly into:

if (_mouseOver ^ isOver) ...
 
B

Berryl Hesh

Hi Stefan
These to operators have a a huge difference in evalutaion:
"||" will return true if either one of the oparands is True or both,
"^" will return True if only exactly one, but not both operands are True.

This was the part I wasn't getting - thank you!

Berryl
 
B

Ben Voigt [C++ MVP]

Berryl Hesh said:
Hi Stefan


This was the part I wasn't getting - thank you!

But what you almost certainly wanted, because it better conveys the intent,
is:

if (_mouseOver != isOver) { ... }
 
M

Michael C

Ben Voigt said:
But what you almost certainly wanted, because it better conveys the
intent, is:

if (_mouseOver != isOver) { ... }

This entire thread wouldn't exist if the previous programmer had done their
job properly and written readable code. :)

Michael
 
A

Arne Vajhøj

Michael said:
Xor is used by programmers who are trying to show off. Not equals is much
readable.

I do not agree.

The traditional comparisons should not be used for bool - or we will
start to see the if(x==true) construct all over.

Arne
 
P

Pavel Minaev

I do not agree.

The traditional comparisons should not be used for bool - or we will
start to see the if(x==true) construct all over.

(x==true) is bad, but there's nothing wrong with (x == y) when bothx
as y are bool, and (x != y) is fine, too (and certainly more people
"get it" compared to ^). And what "traditional comparison" would you
use for equality anyway?
 
A

Arne Vajhøj

Pavel said:
(x==true) is bad, but there's nothing wrong with (x == y) when both x
as y are bool, and (x != y) is fine, too

Not in my very humble opinion.
(and certainly more people
"get it" compared to ^).

People should know what XOR is. But yes - some may not
know that ^ in C# is XOR - and that is a problem.
And what "traditional comparison" would you
use for equality anyway?

I assume you mean what untraditional comparison I would use:
!(x ^ y)

Arne
 
P

Pavel Minaev

People should know what XOR is. But yes - some may not
know that ^ in C# is XOR - and that is a problem.

People do know that ^ is XOR, but very few know that in C# and Java,
for booleans, it's proper boolean XOR, and not bitwise. This is the
legacy of C/C++ that we'll have to deal with for a long time.

Besides, for bools, XOR _is_ "not equals". There's no shame in calling
it what is is :)

One thing I personally actually miss from bitwise ops in C# is the
opposite of XOR, EQV. Hey, that still was in VB6, of the latest
things...
 
B

Ben Voigt [C++ MVP]

People should know what XOR is. But yes - some may not
know that ^ in C# is XOR - and that is a problem.


I assume you mean what untraditional comparison I would use:
!(x ^ y)

Most people do not know what XOR is. Even those who do, should only use it
on booleans when the operation is naturally expressed in terms of XOR (for
example parity calculation). If you're thinking in terms of whether two
booleans are equal, then use the comparison operators.
 
N

Nils Gösche

Ben Voigt said:
Most people do not know what XOR is. Even those who do, should only
use it on booleans when the operation is naturally expressed in terms
of XOR (for example parity calculation). If you're thinking in terms
of whether two booleans are equal, then use the comparison operators.

That seems reasonable: It depends on the situation. Here's another
example where I used the ^ operator on booleans recently:

A class Foo has a method Bar() that returns boolean, and also has a
property IsNegated that »inverts« the behavior of Bar, so to speak:

class Foo
{
...
public bool IsNegated;
public bool Bar()
{
bool ret = ...;
return IsNegated ^ ret;
}
}

I think ^ is easier to understand than != in this situation.

Regards,
 
A

Arne Vajhøj

Peter said:
Any particular reason? Or just an opinion you hold?

Just an opinion.

There are no functional differences.
It seems to me that if you can sort by boolean (and you certainly can),
it makes just as much sense to do simple equality comparisons.

You can - but in my opinion you should not.
Even for people who know what the ^ operator does, I think it makes the
code harder to read. "this variable exclusive OR with this other
variable" is not nearly as direct a way of saying "this variable is not
equal to this other variable", when the latter is exactly what you mean.

I think "exactly one of these two variables is true" is very direct.
And frankly, in spite of its origins in boolean logic, IMHO it's most
useful when applied bit-wise to scalars, precisely because if we're
dealing with just a single bit, we have other operators that provide the
same implementation in a semantically more-accurate way. "Exclusive OR"
really is just a fancy way of saying "not the same".

I consider XOR just as eligible for use as AND, OR and NOT.
Oh, in case it wasn't obvious yet: I agree whole-heartedly with Michael
and Pavel's point of view.

I think most of us got it.

:)

Arne
 
A

Arne Vajhøj

Pavel said:
People do know that ^ is XOR, but very few know that in C# and Java,
for booleans, it's proper boolean XOR, and not bitwise.

Does people not learn tables for boolean operators any more ??

I would expect any programmer to know the table for XOR, the smart
ones to know how to apply the same to bitwise and those reading
a lot to know that it is ^ in C# (and other C family languages).

Arne
 
P

Pavel Minaev

Does people not learn tables for boolean operators any more ??

I would expect any programmer to know the table for XOR, the smart
ones to know how to apply the same to bitwise and those reading
a lot to know that it is ^ in C# (and other C family languages).

They do know the tables. My point is that in C, ^ is always bitwise,
never proper boolean (and you might want to consider why there wasn't
^^ analogous to || and &&). Not that many people with C background
know that in C#, ^ actually works on boolean values the way you'd
expect it to do.
 
M

Michael C

Arne Vajhøj said:
It is boolean operators on booleans.

!(x ^ y) is easier to read than (x == y) ??
That's gotta be the biggest stretch I've seen in a while. Surely you jest.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top