title: Multiplying Large Number in java
body: <p>I am multiplying the <code>2</code> very large number in java , but the multiply output seems to be little strange<br>
  <strong>Code</strong></p>
  
  <pre><code>long a =  2539586720l;
  long b = 77284752003l;
  a*=b;
  System.out.println(a);
  a=(long)1e12;
  b=(long)1e12;
  a*=b;
  System.out.println(a);
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>-6642854965492867616
  2003764205206896640
  </code></pre>
  
  <p>In the first case why the result is negative , if it's because of overflow then how come the result of second is positive ? Please explain this behavior ?
  <a href="http://ideone.com/pmKW0e" rel="nofollow noreferrer">Code </a></p>
  
  <pre><code>Edit:
  </code></pre>
  
  <p>I am using <code>mod=100000000009</code> operation still it's negative ?</p>
  
  <pre><code>  a = ((a%mod)*(b%mod))%mod
  </code></pre>