The Artima Developer Community
Sponsored Link

Inner Int
An int Reveals Its Inner Nature


The Inner Int applet, included below, lets you experiment with the two's-complement format of integers in the Java virtual machine. This applet accompanies Chapter 12, "Integer Arithmetic," of Inside the Java 2 Virtual Machine.

For some reason, your browser won't let you view this way cool Java applet.

A few key-press sequences that illustrate interesting characteristics of two's-complement numbers are given below:

After Max Comes Min
Try this key-press sequence:

++, --, ++, --, ...

This key-press sequence yields overflow when you press either of the ++ or -- buttons. Note that one more than Max is Min, and one less than Min is Max.

Min Magnitude Greater Than Max
Try this key-press sequence:

*=-1, *=-1, *=-1,...

Because the magnitude of the smallest (negative) int is one greater than the magnitude of the largest (positive) int, multiplying the smallest int by -1 yields an overflow. When you press the *=-1 button, the value remains at Min.

Click here to view a page of links to the source code of the Inner Int applet.

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use