Here you can find the source of encode(BitSet origin, int originBitLength)
private static BitSet encode(BitSet origin, int originBitLength)
//package com.java2s; //License from project: Open Source License import java.util.BitSet; public class Main { private static BitSet encode(BitSet origin, int originBitLength) { int encodedBitLength = (int) (originBitLength + Math.log(originBitLength) / Math.log(2) + 1); BitSet encoded = new BitSet(encodedBitLength); for (int i = 1, j = originBitLength - 1; i <= encodedBitLength; ++i) { if (Math.log(i) / Math.log(2) % 1 == 0) { encoded.set(encodedBitLength - i, false); } else { encoded.set(encodedBitLength - i, origin.get(j)); --j;//from w w w . ja v a 2 s . co m } } for (int bitmask = 1, j = 0; j < (int) (Math.log(encodedBitLength) / Math .log(2)) + 1; bitmask <<= 1, ++j) { int countOfBits = 0; for (int i = bitmask; i <= encodedBitLength; ++i) { if ((i & bitmask) != 0) { countOfBits += (encoded.get(encodedBitLength - i) ? 1 : 0); } } countOfBits %= 2; encoded.set(encodedBitLength - bitmask, countOfBits != 0); } return encoded; } }