title: Generating JVM bytecode for a unary not expression
body: <p>Let's say you're writing a Java (or subset-of-Java) compiler and you want to generate bytecode for a unary not expression, <code>!E</code>. You're past type checking so you know <code>E</code> has type <code>boolean</code>, i.e. it will push a <code>1</code> or a <code>0</code> on to the operand stack.</p>
  
  <p>One way to do it is something like (in Jasmin syntax):</p>
  
  <pre><code>E
  ifeq truelabel
  iconst_0
  goto stoplabel
  truelabel:
  iconst_1
  stoplabel:
  </code></pre>
  
  <p>i.e. if there's a 0 on the stack push 1, else push 0. Another way to do it, taking advantage of the fact that a <code>boolean</code> is just an <code>int</code> with value <code>1</code> or <code>0</code>, is to say <code>!E = (E + 1) % 2</code> and generate</p>
  
  <pre><code>E
  iconst_1
  iadd
  iconst_2
  irem
  </code></pre>
  
  <p>Is there an advantage to using one over the other? Or something else entirely?</p>