<h2>Java Integer Arithmetic</h2> <p align=justify> JAVA interger arithmetic (<tt>int</tt> and <tt>long</tt>) cannot overflow or underflow: it simply wraps round and comes full circle. <p><table> <tr><td nowrap> <pre>0001 +1 0000 +0 1111 -1 1110 -2 1101 -3 1100 -4 1011 -5 1010 -6 1001 -7 1000 -8 0111 +7 0110 +6 0101 +5 0100 +4 0011 +3 0010 +2 0001 +1 0000 +0 1111 -1</pre> <td><p align=justify> The figures at the left illustrate the principle of two's complement arithmetic on which all interger arithmetic in JAVA is based. <p align=justify> Notice how the bits change around zero. Minus 1 is all bits set, zero is no bits set, plus 1 is the least significant bit set. Notice too that two's complement arithmetic wraps round from + 7 to -8! One more numerically. <p align=justify> The full range of values is shown for only a 4-bit two's complement register so that the full range can be illustrated in half a page. <p align=justify> To convert a negative number to its positive equivalent and vice versa, invert all the bits then add 1. <p align=justify> Java's <tt>int</tt> is a 32-bit register. Its critical wrapping at zero and at its numerical maxima are shown below. </table> <p>Around zero it behaves as follows: <pre>00000000000000000000000000000010 +2 00000000000000000000000000000001 +1 00000000000000000000000000000000 +0 11111111111111111111111111111111 -1 11111111111111111111111111111110 -2</pre> Around its maximum values it behaves as follows: <pre>10000000000000000000000000000010 -4294967294 10000000000000000000000000000001 -4294967295 10000000000000000000000000000000 -4294967296 01111111111111111111111111111111 +4294967295 01111111111111111111111111111110 +4294967294</pre> <p align=justify> A <tt>short</tt> (16-bit), <tt>int</tt> (32-bit), and <tt>long</tt> (64-bit) arithmetic all follow the same rules. Arithmetic performed on a <tt>char</tt> (16-bit Unicode) converts implicitly to <tt>int</tt> before applying the actual arithmetic operations. <p>The arithmetic operations which can be performed on integers are: <p align=center><table border> <tr><td><tt>x + y</tt><td><tt>add x to y (always wraps)</tt> <tr><td><tt>x - y</tt><td><tt>subtract y from x</tt> <tr><td><tt>x * y</tt><td><tt>multiply x by y</tt> <tr><td><tt>x / y</tt><td><tt>divide x by y</tt> <tr><td><tt>x % y</tt><td><tt>gives the remainder when x is divided by y</tt> </table> <p align=justify> The action of the remainder operator is to repeatedly subtract y from x until it cannot get a complete y. It then returns what is left. Eg: 15 % 4 works out as: <pre>15 - 4 = 11 11 - 4 = 7 7 - 4 = 3 3 - 4 cannot go so it returns 3 as the answer.</pre> <p>The remainder function is defined formally by the identity: <pre>( x / y ) * y + x % y == x</pre> <p>Interger multiplication wraps. Interger division truncates towards zero: eg: <pre> +7 / 2 = +3 and -7 / 2 = -3</pre> <p align=justify> <tt>x / 0</tt> and <tt>x % 0</tt> are invalid and throw an <tt>ArithmeticException</tt>. Java arithmetic also supports the 'minus' unary operator to negate the value to which it is applied eg:<tt> -3</tt> <p>For symmetry it also supports the unary 'plus' operator eg:<tt> +3</tt> <hr> &copy; 1998 Robert John Morton