programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers 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:

# Short Circuit and non-Short Circuit Logical Operator

Greenhorn
Posts: 25

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

Ranch Hand
Posts: 5575

Bartender
Posts: 10575
66

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

Saloon Keeper
Posts: 8307
148
• 1
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:

Marshal
Posts: 57437
175
What about when you need to maintain precedences with ^?
Would you use p & q ^ r & s or (p && q) ^ (r && s)?

Deepak Naithani
Greenhorn
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
Posts: 25

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

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

Campbell Ritchie
Marshal
Posts: 57437
175

Deepak Naithani wrote:

Two instances of count++ I trust nobody would use code like that in real life.

Deepak Naithani
Greenhorn
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
Posts: 10575
66
• 1

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
Marshal
Posts: 57437
175
• 1
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
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
Marshal
Posts: 57437
175
You’re welcome

 Cob is sand, clay and sometimes straw. This tiny ad is made of cob: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS