Sponsored Link •

Advertisement

The Inner Int applet, included below, lets you experiment with the two'scomplement format of integers in the Java virtual machine. This applet accompanies Chapter 12, "Integer Arithmetic," of Inside the Java 2 Virtual Machine.
A few keypress sequences that illustrate interesting characteristics of two'scomplement numbers are given below:
After Max Comes Min
Try this keypress sequence:
Max ++, , ++, , ...
This keypress 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 keypress 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.