title: Bitwise left shift behaviour
body: <p>Today I was learning about the left shift bit operator (<code>&lt;&lt;</code>). As I understand it the left shift bit operator moves bits to the left as specified. And also I know multiply by 2 for shifting. But I am confused, like what exactly is the meaning of &quot;shifting bits&quot; and why does the output differ when value is assigned with a different type?</p>
  <p>When I call the function below, it gives output as <code>System.out.println(&quot;b=&quot;+b); //Output: 0 </code></p>
  <p>And my question is: how does b become 0 and why is b typecasted?</p>
  <pre><code>public void leftshiftDemo()
  {
      byte a=64,b;
      int i;
      i=a &lt;&lt; 2;
      b=(byte)(a&lt;&lt;2);
      System.out.println(&quot;i=&quot;+i); //Output: 256    i.e 64*2^2
      System.out.println(&quot;b=&quot;+b); //Output: 0   how &amp; why b is typecasted
  }
  </code></pre>
  <hr />
  <p><strong>Update</strong> (new doubt):</p>
  <p>what does it mean &quot;If you shift a 1 bit into high-order position (Bit 31 or 63), the value will become negative&quot;. eg.</p>
  <pre><code>public void leftshifHighOrder()
  {
      int i;
      int num=0xFFFFFFE;
  
      for(i=0;i&lt;4;i++)
      {
          num=num&lt;&lt;1;
          System.out.println(num);
          /*
           * Output:
           * 536870908
           * 1073741816
           * 2147483632
           * -32   //how this is -ve?
           */
      }
  }
  </code></pre>