Sponsored Link •
|
Advertisement
|
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.
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:
Max ++, --, ++, --, ...
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:
Min *=-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.