programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# bitwise vs. conditional

Hans Baba
Ranch Hand
Posts: 36
Hello guys,

can someone tell the difference between;
&& and &
|| and | and ^

Thank you!

Knute Snortum
Sheriff
Posts: 4274
127
"&&" and "||" (and "!") deal with booleans, that is true or false. if (condition1 && condition2) { means both condition1 and condition2 must be true. "||" means either condition1 or condition2 or both can be true.

"&", "|", and "^" have to do with moving bits around. Beginning programs don't do this much. Say you have these two values in binary:

0110
1101
----
0100

This is "anding" (&) the two values

0110
1101
----
1111

This is "oring" (|) the two values.

Hans Baba
Ranch Hand
Posts: 36
aha, this makes some sense now. Its good to know that I dont have to deal with bitwise (as a beginner) for now :P they are a bit confusing.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
However, that's not the whole story. You can use & and | also for booleans, not just for bit manipulations.

The difference with && and || in that case is that && and || are short circuiting, while & and | are not.

Knute Snortum
Sheriff
Posts: 4274
127
• 2
Jesper de Jong wrote:However, that's not the whole story. You can use & and | also for booleans, not just for bit manipulations.

The difference with && and || in that case is that && and || are short circuiting, while & and | are not.

But a beginner always wants short-circuiting.

In case you're wondering what "short-circuiting" means, here's an axample:

In this case, both condition1 and condition2 have to be true. So if condition1 is false, why do condition2? You stop evaluating at condition1.

(And just now, I see Jesper gave you a link.)

Stevens Miller
Bartender
Posts: 1445
30
Jesper de Jong wrote:However, that's not the whole story. You can use & and | also for booleans, not just for bit manipulations.

The difference with && and || in that case is that && and || are short circuiting, while & and | are not.

My goodness! I'd have a lot a bet on that last point. Thanks, Jesper.

Stevens Miller
Bartender
Posts: 1445
30
Knute Snortum wrote:But a beginner always wants short-circuiting.

"Always" covers a lot of cases.

I actually learned the hard way about short-circuiting, in my early days with C (which also short-circuits). Imagine something like this:

There are a lot of reasons why that fragment might not be the best way to do what it does, but it wouldn't work at all until "&&" were replaced with the non-short-circuiting "&" operator.

Something like this burned me, many decades ago, until one of my supportive and nurturing colleagues looked over my shoulder at the screen of my VT-100, and soothingly observed that, "That's never gonna work and you shouldn't be doing it that way in the first place."

Knute Snortum
Sheriff
Posts: 4274
127
Stevens Miller wrote:
Knute Snortum wrote:But a beginner always wants short-circuiting.

"Always" covers a lot of cases.

Campbell Ritchie
Marshal
Posts: 56533
172
That C code probably relies on a side‑effect of the right half of the conditional. Now I know old C code had to be optimised for memory use, but most poeple would consider a side‑effect to be at best dubious practice.
I think you should shorten that second methodI would also avoid such read() methods, too. I hate that sort of read() method.

Stevens Miller
Bartender
Posts: 1445
30
Knute Snortum wrote:Indeed. Hyperbole alert initiated.

I have long lamented the lack of a specific emoticon for hyperbole.

Stevens Miller
Bartender
Posts: 1445
30
Campbell Ritchie wrote:That C code probably relies on a side‑effect of the right half of the conditional.

Quite, and that's my point.

...most people would consider a side‑effect to be at best dubious practice.

And those people would include me (although next you're going to have me implicitly agreeing that functional programming is a good thing, and I don't want to go there today). However, as a beginner, side-effects were part of my life.

The fact that && short-circuits is an optimization for speed, more than memory, I believe. I see its virtue, sorta, but it's also a "hidden" asymmetry in the language(s). Logically, (a && b) == (b && a), but not if a or b has, as you (and I) point out, side-effects. Also, with disturbingly increasing frequency, I have been seeing nightmarish code like this

though mostly in shell scripts (thankfully, Java won't compile any such horror).

Campbell Ritchie
Marshal
Posts: 56533
172
Stevens Miller wrote: . . . (thankfully, Java won't compile any such horror).

Stevens Miller
Bartender
Posts: 1445
30
My compiler says, "not a statement" when I try it.

Campbell Ritchie
Marshal
Posts: 56533
172
Thank goodness for that.
boolean b = routine() && conditionalOperation();

Stevens Miller
Bartender
Posts: 1445
30
Campbell Ritchie wrote:Thank goodness for that.