Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having a language reference is one thing, interpreting it correctly is another.</p> <p>We need to interpret things correctly.</p> <p>Even if Java documented that <code>&amp;</code> is both bitwise and logical, we could make an argument that <code>&amp;</code> really didn't lost its logical-operator-ness mojo since time immemorial, since C. That is, <code>&amp;</code> is first and foremost, an inherently logical operator(albeit a non-short-circuited one at that)</p> <p><code>&amp;</code> parses lexically+logically as logical operation.</p> <p>To prove the point, both of these lines behaves the same, eversince C and upto now(Java, C#, PHP, etc)</p> <pre><code>if (a == 1 &amp;&amp; b) if (a == 1 &amp; b) </code></pre> <p>That is, the compiler will interpret those as these:</p> <pre><code>if ( (a == 1) &amp;&amp; (b) ) if ( (a == 1) &amp; (b) ) </code></pre> <p>And even if both variables <code>a</code> and <code>b</code> are both integers. This...</p> <pre><code>if (a == 1 &amp; b) </code></pre> <p>... will still be interpereted as:</p> <pre><code>if ( (a == 1) &amp; (b) ) </code></pre> <p>Hence, this will yield a compilation error on languages which doesn't facilitate integer/boolean duality, e.g. Java and C#:</p> <pre><code>if (a == 1 &amp; b) </code></pre> <p>In fact, on the compilation error above, we could even make an argument that <code>&amp;</code> didn't lost its logical(non-short-circuit) operation mojo, and we can conclude that Java continues the tradition of C making the <code>&amp;</code> still a logical operation. Consequently, we could say it's the other way around, i.e. the <code>&amp;</code> can be repurposed as bitwise operation (by applying parenthesis):</p> <pre><code>if ( a == (1 &amp; b) ) </code></pre> <p>So there we are, in another parallel universe, someone could ask, how to make the <code>&amp;</code> expression become a bitmask operation.</p> <blockquote> <p>How to make the following compile, I read in JLS that <code>&amp;</code> is a bitwise operation. Both a and b are integers, but it eludes me why the following bitwise operation is a compilation error in Java:</p> <p>if (a == 1 &amp; b)</p> </blockquote> <p>Or this kind of question:</p> <blockquote> <p>Why the following didn't compile, I read in JLS that <code>&amp;</code> is a bitwise operation when both its operands are integers. Both a and b are integers, but it eludes me why the following bitwise operation is a compilation error in Java:</p> <p>if (a == 1 &amp; b)</p> </blockquote> <p>In fact, I would not be surprised if there's already an existing stackoverflow questions similar to above questions that asked how to do that masking idiom in Java.</p> <p>To make that logical operation interpretation by the language become bitwise, we have to do this (on all languages, C, Java, C#, PHP, etc):</p> <pre><code>if ( a == (1 &amp; b) ) </code></pre> <p>So to answer the question, it's not because JLS defined things such way, it's because Java(and other languages inspired by C)'s <code>&amp;</code> operator is for all intents and purposes is still a logical operator, it retained C's syntax and semantics. <strong>It's the way it is</strong> since C, since time immemorial, since before I was even born.</p> <p>Things just don't happen by chance, JLS 15.22 didn't happen by chance, there's a deep history around it.</p> <p>In another parallel universe, where <code>&amp;&amp;</code> was not introduced to the language, we will still be using <code>&amp;</code> for logical operations, one might even ask a question today:</p> <blockquote> <p>Is it true, we can use the logical operator <code>&amp;</code> for bitwise operation?</p> </blockquote> <p><code>&amp;</code> <strong>doesn't care</strong> if its operands are integers or not, booleans or not. It's <strong>still a logical operator</strong>, a non-short-circuited one. And in fact, the only way to force it to become a <strong>bitwise operator</strong> in Java(and even in C) is to put parenthesis around it. i.e.</p> <pre><code>if ( a == (1 &amp; b) ) </code></pre> <p>Think about it, if <code>&amp;&amp;</code> was not introduced to C language(and any language who copied its syntax and semantics), anyone could be asking now:</p> <blockquote> <p>how to use <code>&amp;</code> for bitwise operations?</p> </blockquote> <h2>To sum it up, first and foremost Java <code>&amp;</code> is inherently a logical operator(a non-short-circuited one), it doesn't care about its operands, it will do its business as usual(applying logical operation) even if both operands are integers(e.g. masking idiom). You can only force it to become bitwise operation by applying parenthesis. Java continues the C tradition</h2> <p>If Java's <code>&amp;</code> really is a bitwise operation if its operands(integer 1 and integer variable <code>b</code> on example code below) are both integers, this should compile:</p> <pre><code> int b = 7; int a = 1; if (a == 1 &amp; b) ... </code></pre>
    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.
 

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