• Post Reply Bookmark Topic Watch Topic
  • New Topic

bitwise vs. conditional  RSS feed

 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

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



and can you include some examples in your explanations please?

Thank you!
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"&&" 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Knute Snortum wrote:But a beginner always wants short-circuiting.


"Always" covers a lot of cases.



Indeed. Hyperbole alert initiated.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote: . . . (thankfully, Java won't compile any such horror).
Are you sure about that?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler says, "not a statement" when I try it.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank goodness for that.
What about this?
boolean b = routine() && conditionalOperation();
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Thank goodness for that.
What about this?
boolean b = routine() && conditionalOperation();

Oh, well, of course that compiles. What I'm seeing, of late, doesn't seem to involve any confusion between evaluating an expression for its boolean value, and using the short-circuiting behavior of the "&&" operator as a kind of tricky "if" statement. People seem to do one or the other.

Then again, the tricky is strong in those who write long shell scripts. Anything longer than a couple of dozen lines, and I end up switching to C, myself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!