aspose file tools*
The moose likes Beginning Java and the fly likes Short Circuit and non-Short Circuit Logical Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Short Circuit and non-Short Circuit Logical Operator" Watch "Short Circuit and non-Short Circuit Logical Operator" New topic
Author

Short Circuit and non-Short Circuit Logical Operator

Deepak Naithani
Greenhorn

Joined: Aug 30, 2012
Posts: 25

my question is that what is the use of non-Short Circuit Logical Operator " | " any example on this.
Why is this used?

Thanks in advance.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.coderanch.com/t/506151/java-programmer-SCJP/certification/short-circuit-operators hope that helps .
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Deepak Naithani wrote:my question is that what is the use of non-Short Circuit Logical Operator " | " any example on this.

It's not generally used for boolean logic; it's used for logical bitwise operations on numeric values, so both operands must be evaluated.

Eg: n & 1
returns 1 if n is odd, and 0 if n is even.

BTW, your table is kind of meaningless, since logical operators are rarely used with boolean literals; they're used with boolean variables.
So
if (b1 && b2)
returns true only if both b1 and b2 are true; but b2 is only checked if b1 is true.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Non-shorting operators are used if you want to evaluate the right-hand expression regardless of what the outcome of the boolean expression is. This is the case if the right-hand expression has a side effect that should always happen.
helpMom() and helpDad() perform helpful actions and return whether the action succeeded. After you managed to help either mom or dad, you may go play games. However, just because you managed to help mom, it would still be a good idea to help dad even if you are already allowed to play games.

Non-shorting operators should never be used (for things other than bit manipulation). This code is much clearer:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
What about when you need to maintain precedences with ^?
Would you use p & q ^ r & s or (p && q) ^ (r && s)?
Deepak Naithani
Greenhorn

Joined: Aug 30, 2012
Posts: 25
Stephan van Hulst wrote:Non-shorting operators are used if you want to evaluate the right-hand expression regardless of what the outcome of the boolean expression is. This is the case if the right-hand expression has a side effect that should always happen.
helpMom() and helpDad() perform helpful actions and return whether the action succeeded. After you managed to help either mom or dad, you may go play games. However, just because you managed to help mom, it would still be a good idea to help dad even if you are already allowed to play games.

Non-shorting operators should never be used (for things other than bit manipulation). This code is much clearer:


i'm clear about the use of these logical operator (Short Circuit and non-Short Circuit Logical Operator), non-Short Circuit Logical Operator are used to evaluate second part regarlless of whatever is the output of first part but what is the exact difference in use of & and | operator.
Any specific use of these operators.

For Example:

I've gone through this link : http://www.coderanch.com/t/506151/java-programmer-SCJP/certification/short-circuit-operators.
Deepak Naithani
Greenhorn

Joined: Aug 30, 2012
Posts: 25


Try to change if conditions all doubt will be clear for "&" , "|"(inclusive Or) , "^"(exclusive OR "XOR").

int bitmask = 0x78FF;
int val = 0x2266;
how this things are working i'm not getting it cleared.
Thanks for previous help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Deepak Naithani wrote:
Two instances of count++ I trust nobody would use code like that in real life.
Deepak Naithani
Greenhorn

Joined: Aug 30, 2012
Posts: 25
Sir, i knew that code will not be used by anyone but if wanted to clear logic some fun need to be done with code.
So only trying it in examples not for working environment.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Deepak Naithani wrote:i'm clear about the use of these logical operator (Short Circuit and non-Short Circuit Logical Operator), non-Short Circuit Logical Operator are used to evaluate second part regarlless of whatever is the output of first part but what is the exact difference in use of & and | operator.

When used on booleans, exactly the same as the difference between && and ||, except that both operators are always evaluated. Are you asking what the difference between a logical AND and a logical OR is?

The short-circuit versions also work on any primitive numeric type as well as booleans - indeed, that's their primary purpose: to provide those operations for numeric types - which is why they're called 'bitwise' operators. A decent summary of what each one does can be found here; otherwise you can find more general information about them here.

The "short circuit" behaviour you're seeing when they're used with booleans is simply because they treat each boolean like a single bit.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Agreed
It is a good idea to try out bad code, as long as you know it is bad code and you are only trying it out. But on this beginner’s forum you have to be careful; some of the people here have very little experience.
Deepak Naithani
Greenhorn

Joined: Aug 30, 2012
Posts: 25
Winston Gutkowski wrote:
Deepak Naithani wrote:i'm clear about the use of these logical operator (Short Circuit and non-Short Circuit Logical Operator), non-Short Circuit Logical Operator are used to evaluate second part regarlless of whatever is the output of first part but what is the exact difference in use of & and | operator.

When used on booleans, exactly the same as the difference between && and ||, except that both operators are always evaluated. Are you asking what the difference between a logical AND and a logical OR is?

The short-circuit versions also work on any primitive numeric type as well as booleans - indeed, that's their primary purpose: to provide those operations for numeric types - which is why they're called 'bitwise' operators. A decent summary of what each one does can be found here; otherwise you can find more general information about them here.

The "short circuit" behaviour you're seeing when they're used with booleans is simply because they treat each boolean like a single bit.

Winston

after doing above coding, i got the "question" which i was searching for, my question was

Q;- What will be output of "if" 0r "while" condition regardless the use of "Short Circuit and non-Short Circuit Logical Operator"?
i got the point which is was searching

Thanks to all
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Short Circuit and non-Short Circuit Logical Operator