title: Bitwise differences in Java/Perl
body: <p>I was messing around and noticed differences that I don't understand between Java and Perl when I bit-shifted -1.</p>
  
  <p>I thought integers are stored in two's complement binary, so if there are 32 bits, -1 is 11...11 (32 times).<br>
  I'd expect <strong>-1 >> 1</strong> to give $2^31 - 1$ = 011...11 = 2147483647.<br>
  I'd expect <strong>-1 &lt;&lt; 1</strong> to give = 11...110 = -2.</p>
  
  <p>What is the reason for these different behaviors and where are these standards stated in different languages?</p>
  
  <p>Code and printouts for Perl &amp; Java below:</p>
  
  <p>In Perl:</p>
  
  <pre><code>print (-1 &gt;&gt; 1, "\n");
  print (-1 &lt;&lt; 1, "\n");
  </code></pre>
  
  <p><strong>2147483647</strong></p>
  
  <p><strong>4294967294</strong></p>
  
  <p>In Java:</p>
  
  <pre><code>public class Tempy {
      public static void main(String[] args){
          System.out.println( -1 &gt;&gt; 1); 
          System.out.println( -1 &lt;&lt; 1);
      }
  }
  </code></pre>
  
  <p><strong>-1</strong></p>
  
  <p><strong>-2</strong></p>