Java examples for Language Basics:BigInteger
Performing Bitwise Operations with BigInteger
import java.math.BigInteger; public class Main { public static void main(String[] argv) throws Exception { // A negative value byte[] bytes = new byte[] { (byte) 0xFF, 0x00, 0x00 }; // -65536 // A positive value bytes = new byte[] { 0x1, 0x00, 0x00 }; // 65536 BigInteger bi = new BigInteger(bytes); // Get the value of a bit boolean b = bi.testBit(3); // 0 b = bi.testBit(16); // 1 // Set a bit/*from w w w . j a v a 2s. c o m*/ bi = bi.setBit(3); // Clear a bit bi = bi.clearBit(3); // Flip a bit bi = bi.flipBit(3); // Shift the bits bi = bi.shiftLeft(3); bi = bi.shiftRight(1); // Other bitwise operations bi = bi.xor(bi); bi = bi.and(bi); bi = bi.not(); bi = bi.or(bi); bi = bi.andNot(bi); bytes = bi.toByteArray(); } }