Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They(<code>&amp;</code> and <code>|</code>) were used for two purposes long time ago, logical operator and bitwise operator. If you'll check out the neonatal C (the language Java was patterned after), <code>&amp;</code> and <code>|</code> were used as logical operator. </p> <p>But since disambiguating the bitwise operations from logical operations in the same statement is very confusing, it prompted Dennis Ritchie to create a separate operator(<code>&amp;&amp;</code> and <code>||</code>) for logical operator.</p> <p>Check the <strong>Neonatal C</strong> section here: <a href="http://cm.bell-labs.com/who/dmr/chist.html" rel="nofollow noreferrer">http://cm.bell-labs.com/who/dmr/chist.html</a></p> <p>You can still use the <em>bitwise operators</em> as <strong>logical operators</strong>, its retained operator precedence is the evidence of that. Read out the history of bitwise operator's <strong>past life</strong> as logical operator on Neonatal C</p> <p>Regarding the evidence, I made a blog post on comparing the logical operator and bitwise operator. It will be self-evident that the so called bitwise operators are <strong>still</strong> logical operators if you try contrasting them in an actual program: <a href="http://www.anicehumble.com/2012/05/operator-precedence-101.html" rel="nofollow noreferrer">http://www.anicehumble.com/2012/05/operator-precedence-101.html</a></p> <p>I also answered a question related to your question on <a href="https://stackoverflow.com/questions/2773186/what-is-the-point-of-the-logical-operators-in-c/2773257#2773257">What is the point of the logical operators in C?</a></p> <p>So it's true, bitwise operators are logical operators too, albeit non-short-circuited version of short-circuited logical operators.</p> <hr/> <p>Regarding</p> <blockquote> <p>There are already logical operators &amp;&amp;, ||, then why use &amp;, |, ^?</p> </blockquote> <p>The XOR can be easily answered, it's like a radio button, only one is allowed, code below returns false. Apology for the contrived code example below, the belief that drinking both beer and milk at the same time is bad was <a href="http://answers.yahoo.com/question/index?qid=20090516195352AAvdZEf" rel="nofollow noreferrer">debunked already</a> ;-)</p> <pre><code>String areYouDiabetic = "Yes"; String areYouEatingCarbohydrate = "Yes"; boolean isAllowed = areYouDiabetic == "Yes" ^ areYouEatingCarbohydrate == "Yes"; System.out.println("Allowed: " + isAllowed); </code></pre> <p>There's no short-circuit equivalent to XOR bitwise operator, as both sides of the expression are needed be evaluated.</p> <p>Regarding why the need to use <code>&amp;</code> and <code>|</code> bitwise operators as logical operators, frankly you'll be hard-pressed to find a need to use bitwise operators(a.k.a. non-short-circuit logical operators) as logical operators. A logical operation can be non-short-circuited (by using the bitwise operator, aka non-short-circuited logical operator) if you want to achieve some side effect and make your code compact(subjective), case in point:</p> <pre><code>while ( !password.isValid() &amp; (attempts++ &lt; MAX_ATTEMPTS) ) { // re-prompt } </code></pre> <p>The above can re-written as the following(removing the parenthesis), and still has exactly the same interpretation as the preceding code.</p> <pre><code>while ( !password.isValid() &amp; attempts++ &lt; MAX_ATTEMPTS ) { // re-prompt } </code></pre> <p>Removing the parenthesis and yet it still yields the same interpretation as the parenthesized one, can make the <strong>logical operator vestige</strong> of <code>&amp;</code> more apparent. To run the risk of sounding superfluous, but I have to emphasize that the unparenthesized expression is not interpreted as this:</p> <pre><code>while ( ( !password.isValid() &amp; attempts++ ) &lt; MAX_ATTEMPTS ) { // re-prompt } </code></pre> <p>To sum it up, using <code>&amp;</code> operator (more popularly known as bitwise operator only, but is actually both bitwise and logical(non-short-circuited)) for non-short-circuit logical operation to achieve side effect is clever(subjective), but is not encouraged, it's just one line of savings effect in exchange for readability.</p> <p>Example sourced here: <a href="https://stackoverflow.com/questions/9264897/reason-for-the-exsistance-of-non-short-circuit-logical-operators/9265249#9265249">Reason for the exsistance of non-short-circuit logical operators</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload