Java examples for java.util:BitSet
Convert a BigInteger into a BitSet.
import java.math.BigInteger; import java.util.BitSet; import java.util.Map; public class Main{ public static void main(String[] argv) throws Exception{ BigInteger i = new BigInteger("1234"); System.out.println(bigIntToBitSet(i)); }//w w w . j ava 2s . c o m /** * Convert a BigInteger into a BitSet. * @param i BigInteger to convert. * @return */ public static BitSet bigIntToBitSet(BigInteger i) { return BitSetUtils.fromByteArray(i.toByteArray()); } public static BitSet fromByteArray(byte[] bytes) { BitSet bits = new BitSet(); for (int i = 0; i < bytes.length * 8; i++) { if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) { bits.set(i); } } return bits; } public static byte[] toByteArray(BitSet bits) { byte[] bytes = new byte[bits.length() / 8 + 1]; for (int i = 0; i < bits.length(); i++) { if (bits.get(i)) { bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8); } } return bytes; } }