Presents your JAVA E-NEWSLETTER for March 10, 2003 <-------------------------------------------> MANIPULATE LARGE BITS WITH BIGINTEGER Bitwise logic in programs usually involves shifting a number back and forth and performing bitwise operations on bits to determine which ones are set. If you need to manipulate numbers with 64 bits or less, you can use a long or an int. If you need to manipulate numbers with more than 64 bits, you'll need to use the java.math.BigInteger class. (BigInteger is part of the core API and has been since 1.1.) http://java.sun.com/j2se/1.3/docs/api/java/math/BigInteger.html The BigInteger type is used for manipulating large integers. There are several constructors, but the most straightforward one takes a java.lang.String object representing the number you want to manipulate. For example: BigInteger bi = new BigInteger("FFFFFFFFFFFFFFF", 16); In the preceding code, an instance of BigInteger was created using the BigInteger constructor that takes a String and an integer representing the number's radix. Once you've created your instance, you can perform operations on it by calling any of the methods provided. Like java.lang.String, the BigInteger class is immutable, so all of the methods that perform operations return an instance of a new BigInteger that is the result of the operation. There are methods for all of the basic math operations--add, subtract, divide, multiply--as well as all of the bit operations you'll need--and, or, xor, andNot, shiftRight, shiftLeft, etc. Here are some method examples: BigInteger result = bi.multiply(new BigInteger("2")); System.out.println(result); result = bi.divide(new BigInteger("2")); System.out.println(result); result = bi.add(new BigInteger("232")); System.out.println(result); result = bi.subtract(new BigInteger("23122")); System.out.println(result); result = bi.shiftRight(10); System.out.println(result); Within the BigInteger class, there are other useful constructors and many other methods that can make your big number manipulation easier. ----------------------------------------