Example usage for java.util BitSet get

List of usage examples for java.util BitSet get

Introduction

In this page you can find the example usage for java.util BitSet get.

Prototype

public boolean get(int bitIndex) 

Source Link

Document

Returns the value of the bit with the specified index.

Usage

From source file:jBittorrentAPI.utils.Utils.java

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[i / 8] |= 1 << (7 - i % 8);
        }/*from  ww  w .  j a  v a2  s. c  o  m*/
    }
    return bytes;
}

From source file:de.uniba.wiai.lspi.chord.data.ID.java

private static void partHashSet(final BitSet dst, final int dstOffset, final BitSet src, final int srcLen) {
    assert (dstOffset + srcLen <= dst.size());
    for (int i = 0; i < Math.min(src.size(), srcLen); ++i)
        dst.set(dstOffset + i, src.get(i));
}

From source file:de.uniba.wiai.lspi.chord.data.ID.java

private static BitSet bitsetResize(final BitSet a, final int numBits) {
    assert (numBits > 0);
    final BitSet b = new BitSet(numBits);
    for (int i = 0; i < Math.min(a.size(), numBits); ++i)
        b.set(i, a.get(i));

    return b;/* w  ww . j a  v  a 2s  .c o  m*/
}

From source file:Main.java

public static byte[] unencodedSeptetsToEncodedSeptets(byte[] septetBytes) {
    byte[] txtBytes;
    byte[] txtSeptets;
    int txtBytesLen;
    BitSet bits;
    int i, j;// w w  w.  j  a va  2s.co m
    txtBytes = septetBytes;
    txtBytesLen = txtBytes.length;
    bits = new BitSet();
    for (i = 0; i < txtBytesLen; i++)
        for (j = 0; j < 7; j++)
            if ((txtBytes[i] & (1 << j)) != 0)
                bits.set((i * 7) + j);
    // big diff here
    int encodedSeptetByteArrayLength = txtBytesLen * 7 / 8 + ((txtBytesLen * 7 % 8 != 0) ? 1 : 0);
    txtSeptets = new byte[encodedSeptetByteArrayLength];
    for (i = 0; i < encodedSeptetByteArrayLength; i++) {
        for (j = 0; j < 8; j++) {
            txtSeptets[i] |= (byte) ((bits.get((i * 8) + j) ? 1 : 0) << j);
        }
    }
    return txtSeptets;
}

From source file:org.springframework.social.oauth1.SigningSupport.java

private static byte[] encode(byte[] source, BitSet notEncoded) {
    Assert.notNull(source, "'source' must not be null");
    ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length * 2);
    for (int i = 0; i < source.length; i++) {
        int b = source[i];
        if (b < 0) {
            b += 256;//from www. j a  v  a 2  s . com
        }
        if (notEncoded.get(b)) {
            bos.write(b);
        } else {
            bos.write('%');
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
            bos.write(hex1);
            bos.write(hex2);
        }
    }
    return bos.toByteArray();
}

From source file:de.uniba.wiai.lspi.chord.data.ID.java

private static BitSet bitsetArithPwrOf2(final BitSet _bs, final int pwrOf2, final boolean addition) {
    // The bitset is defined as big-endian unsigned, i.e. (idx 0 is highest bit)
    final BitSet bs = (BitSet) _bs.clone();
    assert (pwrOf2 < kTotalBitLen);
    for (int i = kTotalBitLen - 1 - pwrOf2; i >= 0; --i) {
        bs.flip(i);//  ww  w .j  a va2 s  .c o  m
        // add/sub for unsigned binary is the same, except we bail if (set == addition) after flip
        if (bs.get(i) == addition)
            break;
    }

    return bs;
}

From source file:URLCodec.java

/**
 * Encodes an array of bytes into an array of URL safe 7-bit 
 * characters. Unsafe characters are escaped.
 *
 * @param urlsafe bitset of characters deemed URL safe
 * @param bytes array of bytes to convert to URL safe characters
 * @return array of bytes containing URL safe characters
 *///ww w.  j av  a 2 s  .  co m
public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    if (urlsafe == null) {
        urlsafe = WWW_FORM_URL;
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b < 0) {
            b = 256 + b;
        }
        if (urlsafe.get(b)) {
            if (b == ' ') {
                b = '+';
            }
            buffer.write(b);
        } else {
            buffer.write('%');
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
            buffer.write(hex1);
            buffer.write(hex2);
        }
    }
    return buffer.toByteArray();
}

From source file:info.rmarcus.birkhoffvonneumann.MatrixUtils.java

public static double permanent(double[][] input) {
    int n = input.length;

    double collector = 0.0;
    Iterator<BitSet> i = bitStringsOfSize(n);
    while (i.hasNext()) {
        BitSet nxt = NullUtils.orThrow(i.next(), () -> new BVNRuntimeException("Iterator returned null!"));
        if (nxt.cardinality() == 0)
            continue;

        int mult = (int) Math.pow(-1, nxt.cardinality());
        double accum = 1.0;

        for (int row = 0; row < n; row++) {
            double x = 0;
            for (int col = 0; col < n; col++) {
                if (!nxt.get(col))
                    continue;

                x += input[row][col];/* w  w w.j  av  a  2 s  .  c o m*/
            }

            accum *= x;
        }

        collector += mult * accum;
    }

    int mult = (int) Math.pow(-1, n);
    return mult * collector;

}

From source file:Main.java

public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast) {
    byte newBytes[];
    BitSet bitSet;
    int i, j, value1, value2;
    bitSet = new BitSet(octetBytes.length * 8);
    value1 = 0;/*from   ww w.  ja  v a2 s.co m*/
    for (i = 0; i < octetBytes.length; i++)
        for (j = 0; j < 8; j++) {
            value1 = (i * 8) + j;
            if ((octetBytes[i] & (1 << j)) != 0)
                bitSet.set(value1);
        }
    value1++;
    value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
    if (value2 == 0)
        value2++;
    newBytes = new byte[value2];
    for (i = 0; i < value2; i++) {
        for (j = 0; j < 7; j++) {
            if ((value1 + 1) > (i * 7 + j)) {
                if (bitSet.get(i * 7 + j)) {
                    newBytes[i] |= (byte) (1 << j);
                }
            }
        }
    }
    if (discardLast && octetBytes.length * 8 % 7 > 0) {
        // when decoding a 7bit encoded string 
        // the last septet may become 0, this should be discarded
        // since this is an artifact of the encoding not part of the 
        // original string
        // this is only done for decoding 7bit encoded text NOT for
        // reversing octets to septets (e.g. for the encoding the UDH)
        if (newBytes[newBytes.length - 1] == 0) {
            byte[] retVal = new byte[newBytes.length - 1];
            System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
            return retVal;
        }
    }
    return newBytes;
}

From source file:org.caleydo.core.util.impute.KNNImpute.java

private static Pair<List<Gene>, List<Gene>> split(List<Gene> neighborhood, BitSet partOf_a) {
    final int a_n = partOf_a.cardinality();
    final int b_n = neighborhood.size() - a_n;

    final List<Gene> a = new ArrayList<>(a_n);
    final List<Gene> b = new ArrayList<>(b_n);

    for (int i = 0; i < neighborhood.size(); ++i) {
        if (partOf_a.get(i))
            a.add(neighborhood.get(i));//from   www  . ja v  a  2 s  .c  om
        else
            b.add(neighborhood.get(i));
    }
    return Pair.make(a, b);
}