Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Octal notation

Java 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.

Table 2-2 Octal digit binary bit patterns

3-bit binary pattern Octal digit

000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

Although the words octal and base eight sound like they should be closely related to the eight bits in a byte, in reality they’re 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 aren’t 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;


Note:  I can think of no reason why you might want to do this. If you do this, please write and tell me why.

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));


Note:  The Byte and Short classes do not have toOctalString() methods, but bytes and shorts can be converted using the Integer.toOctalString() method.

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);

Integers

An 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. There’s 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.

Java’s integer data types map pretty closely to the mathematical ideal, with the single exception that they’re 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.

ints

In 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 one’s place, the second from the right is the two’s place, the third from the right is the four’s, the fourth from the right is the eight’s, and so on, up to the 31st place from the left, which is the 1,073,741,824’s 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.

You’re 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 isn’t used for place value. It’s 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 two’s complement binary numbers. In a two’s 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 I’ve 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


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.