Deepak Naithani wrote:my question is that what is the use of non-Short Circuit Logical Operator " | " any example on this.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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:
Two instances of count++ I trust nobody would use code like that in real life.Deepak Naithani 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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|