![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
One of the advantages of twos complement numbers is that the procedure reverses itself. You dont 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 isnt 2,147,483,648. Its -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; theyre just larger ones. Long, short, and byteSo far weve worked with 32-bit ints. Java provides three other integer data types: byte, short, and long. These have different bit-widths, and theyre not as easy to use as literals in Java source code, but their analysis is exactly the same as that of ints.
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 wont even let you write code like the following: byte b3 = b1 + b2; If you try this, where b1 and b2 are byte variables, youll 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; youll 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 youre 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.
|
![]() |
|