• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short Circuit and non-Short Circuit Logical Operator

 
Deepak Naithani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 6128
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Naithani wrote:
Two instances of count++ I trust nobody would use code like that in real life.
 
Deepak Naithani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic