• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

logical operators..

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi can any one please explain the exact difference between
shortcircuitoperators(| |, &&) and bitwise operators(&,|)
with some code for me to understand...
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
| | and && are logical operators, so you can apply them to a condition only (that are boolean type in java).
| and & operators are bitwise, they produce integer result.
if ((a==0) | | (b==0)) // true if b is 0 OR a is 0
if ((a>b) && (b<0)) // true if a greater then b AND b less than zero
As for bitwise operators, there is a standart approach for telling what they mean.
Each bit in left operand compared to the same bit in the right operand and depeding on this two values result is created.
left bit | right bit | & | | |
------------------------------
0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 0 | 0 | 1 |
1 | 1 | 1 | 1 |
==============================
Actually, this operators have the same idea. If you
take (0) as (false) and (1) as (true) you'll see that the
logic is the same.
0xf & 0x0 == 0x0
1111 &
0000 =
0000
0x5 | 0xa == 0xf
0101 |
1010 =
1111

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, note the "short circuit" behaviour of these operators. If the expression evaluator "knows" that the result is definately true or false after only evaluating part of the expression, then it doesn't bother evaluating the rest of it.
This is really useful in cases like:
<pre>
if (text != null && text.length() > 3)
</pre>
The system first checks to see if text is null. If it is null, then the result of the expression is going to be false, regardless of the result of the second part. So the second part is never evaluated. Which is good in this case, because if text is null, then trying to get the length by calling the 'length' method will generate a NullPointerException.
So this behaviour "short-circuits" the expression.
Be wary, though. Especially if you are used to programming in Pascal or another language which doesn't "short-circuit" expressions. Don't ever put an expression with side-effects in a later part of an expression unless you are happy for it not to be done.
The following, somewhat strange, example:
<pre>
int from = 0;
int to = 5
boolean keepgoing = true;
while (keepgoing==true | | ++from < 5 && --to > 0)
{
data[to] = data[from]
if (data[from].equals("empty")) keepgoing = false;
}
</pre>
Will stay in the loop forever if data[0] is not "empty". It never gets to the code incrementing 'from' or decrementing 'to',
so it never gets to check any other data elements.
This example may seem arbitrary or even unlikely, but please keep it in mind next time you think you are going mad when your program doesn't work.
[This message has been edited by Frank Carver (edited May 30, 2000).]
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, and there is one mostly common usage for shourtcircuit in Java. If you got an object and you want to do something with it, you may want to check if is it null or no:
void do(MyObject x) {
if ((x!=null) && x.goodEnough()) {
// do something
}
}
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic