![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Octal notationJava also allows the use of a base-eight notation with eight digits called octal notation. An octal digit can be represented in three bits. For example, 011 is octal 3. Table 2-2 lists all the octal digits and their equivalent binary patterns. Notice that this is the same as the first eight rows of Table 2-1 with the initial zero removed from each bit pattern.
Although the words octal and base eight sound like they should be closely related to the eight bits in a byte, in reality theyre not. You cannot write a byte value as a certain number of octal digits because the three bits in an octal digit do not evenly divide the eight bits in a byte. Therefore, octal numbers arent nearly as useful in practice as hexadecimal numbers. Their presence in Java is a holdover from their presence in C. Octal numbers were included in C because they are quite useful on machines with 12-bit words. The three bits in an octal number divide evenly into 12 bits, and computers with 12-bit words were still being used when C was created. To use an octal literal in Java code, just prefix it with a leading 0. For example, to set n to decimal 227, you could write int n = 0343;
Java has several methods to convert between decimal and octal notation. The Integer and Long classes each have a static toOctalString() method which converts ints and longs respectively to octal strings. For example, to print the int value 1024 as an octal string, you could write System.out.println(Integer.toOctalString(1024)); Longs are converted similarly: System.out.println(Long.toOctalString(5000000000L));
You can convert an octal string to a numeric value using the parse and valueOf() methods described in the last section. Just pass 8 as the base argument instead. For example: byte b = Byte.parseByte("30", 8); short s = Short.parseShort("7002", 8); int i = Integer.parseInt("30047132", 8); long l = Long.parseLong("0108755260027112", 8); Byte B = Byte.valueOf("30", 8); Short S = Short.valueOf("7002", 8); Integer I = Integer.valueOf("30047132", 8); Long L = Long.valueOf("0108755260027112", 8); IntegersAn integer is a mathematical concept that describes a whole number. One, two, zero, 72, -1,324, and 768,542,188,963,243,888 are all examples of integers. Theres no limit to the size of an integer. An integer can be as large or as small as it needs to be, although it must always be a whole number like seven and never a fraction like seven and a half. Javas integer data types map pretty closely to the mathematical ideal, with the single exception that theyre all of finite magnitude. The four integer types byte, short, int, and long differ in the size of the numbers they can hold, but they all hold only a finite number of different integers. Most of the time this is enough. intsIn Java, an int is composed of four bytes of memory that is, 32 bits. Written in binary notation, an integer looks like 01001101000000011100101010001101 In hexadecimal notation, this same number is 8D01BA8D Each of the rightmost 31 places is a place value. The rightmost place is the ones place, the second from the right is the twos place, the third from the right is the fours, the fourth from the right is the eights, and so on, up to the 31st place from the left, which is the 1,073,741,824s place. The largest possible int in Java has all bits set to one except the leftmost bit. In other words, it is 01111111111111111111111111111111, or, in decimal, 2,147,483,647. Youre probably thinking that we could set the leftmost bit to one, and then have 11111111111111111111111111111111 as the largest number, but the leftmost bit in an int isnt used for place value. Its used to indicate the sign of the number and is called the sign bit. If the leftmost bit is one, then the int is a negative number. Therefore, 11111111111111111111111111111111 is not 4,294,967,295 but rather -1. Java, like most modern computers, uses twos complement binary numbers. In a twos complement scheme, to reverse the sign of a number, you first take its complement that is, convert all the ones to zeroes and all the zeroes to ones and then add one. For example, to convert the byte value 0100001 (decimal 65) to -65, you would follow these steps: 65: 0100001 65 complement: 1011110 Add 1: +0000001 -65: 1011111 Here Ive worked with 8-bit numbers instead of the full 32-bit ints used by Java. The principle is the same regardless of the number of bits in the number. To change a negative number into a positive number, do exactly the same thing. For example: -65: 1011111 -65 complement: 0100000 Add 1: +0000001 65: 0100001
|
![]() |
|