Java BitSet encode(BitSet origin, int originBitLength)

Here you can find the source of encode(BitSet origin, int originBitLength)

Description

encode

License

Open Source License

Declaration

private static BitSet encode(BitSet origin, int originBitLength) 

Method Source Code

//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;
    }
}

Related

  1. decode(BitSet encoded, int encodedBitLength)
  2. decodeIndex(final BitSet bs, final long rangePerDimension)
  3. deleteBits(BitSet bs, BitSet bsDelete)
  4. dualNext(BitSet b)
  5. encode(BitSet allowedCharacters, String s, String charset)
  6. encode(String s, BitSet bs)
  7. ensureExclusivity(BitSet bs1, BitSet bs2)
  8. escape(String s, BitSet safeChars)
  9. expand(final BitSet bits, final BitSet add)