title: Why are bitwise operators slower at comparing booleans than the "normal" ones in Java?
body: <p>Assume the following: You got two functions, both doing basically the same thing, which is comparing two random booleans with AND and OR operators. But the one function does this with the normal conditional operators &amp;&amp; and ||, the other one with the bitwise operators &amp; and |. </p>
  
  <p>I thought that those two functions would of course take the same time to complete, <strong>but</strong> they dont. The one with bitwise comparison takes one fifth more time than the one with the "normal" conditional operator. </p>
  
  <p>I was confused and did some research and found the following definition for the conditional operators on the Java documentation from Oracle:</p>
  
  <pre><code>The &amp;&amp; and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
  &amp;&amp; Conditional-AND
  || Conditional-OR
  </code></pre>
  
  <p>and for the bitwise operator:</p>
  
  <pre><code>The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. 
  The bitwise &amp; operator performs a bitwise AND operation.
  
  The bitwise | operator performs a bitwise inclusive OR operation.
  </code></pre>
  
  <p>Well, those definitions didn't really satisfy me and my question was still not answered. 
  So I did some more research and found a question here on stackoverflow about the effect of bitwise operators on booleans in Java (<a href="https://stackoverflow.com/questions/1724205/effect-of-a-bitwise-operator-on-a-boolean-in-java">Effect of a Bitwise Operator on a Boolean in Java</a>), but that didnt really answer my question either.</p>
  
  <p>Here is the code I tested it with, it uses a random number generator to get random booleans to compare with one another and loops over that a loooot of times. </p>
  
  <pre><code>import java.util.Random;
  
  public class Main {
  
      private static final long LOOPS = 100000000L;
      private static Random rng = new Random();
  
      public static final void main(String[] args)
      {
          System.out.println("Bitwise operator: "+loopWithBitwiseOperator(LOOPS));
          System.out.println("Normal operator: "+loopWithNormalOperator(LOOPS));
      }
  
      public static long loopWithNormalOperator(long loops)
      {
          long startTime = System.currentTimeMillis();
  
          for (long i = 0L; i &lt; loops; i++)
          {
              if (rng.nextBoolean() || rng.nextBoolean())
              {
                  i++;
              }
  
              if (rng.nextBoolean() &amp;&amp; rng.nextBoolean())
              {
                  i++;
              }
          }
  
          return System.currentTimeMillis()-startTime;
      }
  
      public static long loopWithBitwiseOperator(long loops)
      {
          long startTime = System.currentTimeMillis();
  
          for (long i = 0L; i &lt; loops; i++)
          {
              if (rng.nextBoolean() | rng.nextBoolean())
              {
                  i++;
              }
  
              if (rng.nextBoolean() &amp; rng.nextBoolean())
              {
                  i++;
              }
          }
  
          return System.currentTimeMillis()-startTime;
      }
  }
  </code></pre>
  
  <p>I received the following output: </p>
  
  <pre><code>Bitwise operator: 2502
  Normal operator: 1806
  </code></pre>
  
  <p>So the bitwise operator is in fact slower than the "normal" one. My first idea was that maybe the generated boolean by the random number generator is somehow treated different at runtime and when compared with another boolean it actually bitwisely compares more than needed, as it is undefined how it is actually saved. But then I thought that it might happen because of the "short-circuiting" mechanism for conditional operators, which maybe doesnt apply to bitwise operators which probably actually need both values for comparison.</p>
  
  <p>Is my assumption correct? Why does this happen? Another example-specific behavior that doesnt represent the actual behavior of Java's logical operators? </p>
  
  <p>As always, thanks for any help and clarification in advance.</p>
  
  <p>EDIT:</p>
  
  <p>As requested in the comments, I tried to switch the calls around and include some kind of a warm up phase, but still not much of a change:</p>
  
  <pre><code>Normal operator: 1801
  Bitwise operator: 2433
  </code></pre>