Java BitSet longFrom(final BitSet bitSet)

Here you can find the source of longFrom(final BitSet bitSet)

Description

Creates an long out of a bitset

License

Apache License

Parameter

Parameter Description
bitSet the bitset

Return

a long from the bitset representation

Declaration

public static long longFrom(final BitSet bitSet) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.BitSet;

public class Main {
    static final private byte NBITS_LONG_REPRESENTATION = 64;

    /**//ww w . j a va  2s  . c om
     * Creates an long out of a bitset
     *
     * @param bitSet the bitset
     * @return a long from the bitset representation
     */
    public static long longFrom(final BitSet bitSet) {
        return longFrom(bitSet, NBITS_LONG_REPRESENTATION);
    }

    /**
     * Cretes an integer with any number of bits (up to 64 -- long precision) from a bitset
     *
     * @param bitSet the bitset
     * @param nBits  the number of bits to be used for this representation
     * @return an integer with nBits from the bitset representation
     */
    public static long longFrom(final BitSet bitSet, final int nBits) {
        long number = 0;
        for (int bitIndex = bitSet.nextSetBit(0); bitIndex >= 0
                && bitIndex <= nBits; bitIndex = bitSet.nextSetBit(bitIndex + 1))
            number |= 1L << bitIndex;

        return number;
    }
}

Related

  1. isHammingDistanceOne(BitSet a, BitSet b)
  2. isSet(int n, BitSet... sets)
  3. isSubset(BitSet bits1, BitSet bits2)
  4. isSubset(BitSet subsetToTest, BitSet supersetToTest)
  5. isSubSet(BitSet x, BitSet y)
  6. longToBitSet(long bits)
  7. longToBitSet(long value)
  8. mapToBitSet(Map map, String key)
  9. max(BitSet bits)