Brought to you by EarthWeb
ITKnowledge Logo Login Graphic IBM DB2 Universal Database.
IBM DB2 Universal Database.
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


One of the advantages of two’s complement numbers is that the procedure reverses itself. You don’t need separate circuits to convert a negative number to a positive one.

Computer integers differ from the mathematical ideal in that they have maximum and minimum sizes. The largest positive integer has a zero bit on the left side and all remaining bits set to one — that is, 0111111111111111-1111111111111111, or 2,147,483,647 in decimal. If you try to add one to this number as shown here, the one carries all the way over into the leftmost digit. In other words, you get 10000000000000000000000000000000, which is the smallest negative int in Java, decimal -2,147,483,648.

          01111111111111111111111111111111
        + 00000000000000000000000000000001
          10000000000000000000000000000000

Further addition will make the negative number count back up to zero and then into the positive numbers. In other words, if you count high enough, eventually you wrap around to very small numbers. The next int after 2,147,483,647 isn’t 2,147,483,648. It’s -2,147,483,648. If you need to count higher than 2,147,483,647 or lower than -2,147,483,648, then you need to use a long or a floating-point number, as I discuss in the next sections. These numbers have maximums and minimums of their own; they’re just larger ones.

Long, short, and byte

So far we’ve worked with 32-bit ints. Java provides three other integer data types: byte, short, and long. These have different bit-widths, and they’re not as easy to use as literals in Java source code, but their analysis is exactly the same as that of ints.

One’s Complement

Some early computers used one’s complement arithmetic instead. In one’s complement, you invert all the bits to change the sign of a number, as you do in two’s complement, but you don’t add 1. Thus, since 65 is 0100001 and -65 is 1011110. This seems simpler. However, you encounter a problem with zero. Zero itself is 00000000. Negative zero is 11111111. But negative zero is supposed to be the same as positive zero. Adding one to 11111111, as you do in two’s complement, flips all the bits back to 0 as the one carries across to the left and disappears. In two’s complement notation, therefore, 0 and -0 have the same bit pattern. This advantage has led to the triumph of two’s complement computers in the marketplace. One’s complement computers died off even before 12-bit word machines did.

A byte is eight bits wide. The largest byte is 01111111, or 127 in decimal. The smallest byte is 10000000, or -128 in decimal. Bytes are the lowest common denominator for data interchange between different computers, and Java uses them extensively in input and output. However, it does not use byte values in arithmetic calculations or as literals. The Java compiler won’t even let you write code like the following:

     byte b3 = b1 + b2;

If you try this, where b1 and b2 are byte variables, you’ll get an error message that says Error: Incompatible type for =. Explicit cast needed to convert int to byte. This is because the Java compiler converts bytes to ints before doing the calculation. It does not add b1 and b2 as bytes, but rather as ints. The result it produces and tries to assign to b3 is also an int.

Shorts are 16 bits wide. The largest short is 0111111111111111, or 32,767 in decimal. The smallest short is 1000000000000000, or -32,768 in decimal. There is no way to use a short as a literal or in arithmetic. As with bytes, if you write code like

     short s3 = 454 + -732;

you’ll get an error message that says: Error: Incompatible type for =. Explicit cast needed to convert int to short. The Java compiler converts all shorts to ints before doing the calculation. The only time shorts are actually used in Java is when you’re reading or writing data that is interchanged with programs written in other languages on platforms that use 16-bit integers. For example, some old 680X0 Macintosh C compilers use 16-bit integers as the native int format. Shorts are also used when very many of them need to be stored and space is at a premium (either in memory or on disk).

The final Java integer data type is the long. A long is 64 bits wide and can represent integers between -9,223,372,036,854,775,808 and 9,223, 372,036,854,775,807. Unlike shorts and bytes, longs are directly used in Java literals and arithmetic. To indicate that a number is a long, just suffix it with the letter L — for example, 2147483856L or -76L. Like other integers, longs can be written as hexadecimal and octal literals — for example, 0xCAFEBABEL or 0714L.


Note:  You can use either a small l or a capital L to indicate a long literal. However, a capital L is strongly preferred because the lowercase l is easily confused with the numeral 1 in most typefaces.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.