Java BitSet Create convert(final long value)

Here you can find the source of convert(final long value)

Description

Converts a given Long to a BitSet.

License

Open Source License

Parameter

Parameter Description
value The given value.

Return

The BitSet representation of the given value.

Declaration

public static BitSet convert(final long value) 

Method Source Code

//package com.java2s;
/**/*from www  .j a  v  a2  s. c o m*/
 *  Copyright (C) 2012 Ulrich Viefhaus
 *    This file (ByteUtils.java) is from the package utils which is part of Hashchecker.
 *    Hashchecker is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Hashmaker and Hashviewer is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Hashmaker and Hashviewer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

import java.util.BitSet;

public class Main {
    /**
     * Converts a given Long to a BitSet.
     * 
     * @param value
     *            The given value.
     * @return The BitSet representation of the given value.
     */
    public static BitSet convert(final long value) {

        long tmp = value;
        BitSet bits = new BitSet();
        int index = 0;
        while (tmp != 0L) {
            if (tmp % 2L != 0) {
                bits.set(index);
            }
            ++index;
            tmp = tmp >>> 1;
        }
        return bits;
    }

    /**
     * Converts a BitSet to a Long.
     * 
     * @param bits
     *            The given BitSet.
     * @return The long representation of the BitSet.
     */
    public static long convert(final BitSet bits) {

        long value = 0L;
        for (int i = 0; i < bits.length(); ++i) {
            value += bits.get(i) ? (1L << i) : 0L;
        }
        return value;
    }
}

Related

  1. bitSetOf(String inString)
  2. bitSetOfIndexes(int... indexes)
  3. Byte2BitSet(byte bytes)
  4. byte2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended)
  5. byte2BitSet(byte[] b, int offset, int maxBits)
  6. convert(long value, int bitSetOffset, BitSet bits)
  7. convertIntToBitSet(int value)