Java BitSet intToBitSet(int value)

Here you can find the source of intToBitSet(int value)

Description

Convert an integer to BitSet.

License

Open Source License

Parameter

Parameter Description
value Value to convert.

Declaration

public static BitSet intToBitSet(int value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.BitSet;

public class Main {
    /**//from  w w  w.j  av  a2 s  . c o  m
     * Convert an integer to BitSet.
     * @param value Value to convert.
     * @return 
     */
    public static BitSet intToBitSet(int value) {
        BitSet bits = new BitSet();
        int index = 0;
        while (value != 0) {
            if (value % 2 != 0) {
                bits.set(index);
            }
            ++index;
            value = value >>> 1;
        }

        return bits;

    }

    /**
     * Stores an integer number in a BitSet object.
     *
     * @param value integer number to store
     * @param length number of bits to use.
     * @return
     */
    public static BitSet intToBitSet(int value, int length) {
        BitSet bits = new BitSet(length);
        int index = 0;
        while (value != 0) {
            if (value % 2 != 0) {
                bits.set(index);
            }
            ++index;
            value = value >>> 1;
        }

        return bits;
    }
}

Related

  1. hex2BitSet(byte[] b, int offset, int maxBits)
  2. hexToBits(String s, BitSet ba, int length)
  3. increment(BitSet bits, int size)
  4. insertBits(BitSet fromSet, int insertBefore, int count)
  5. intArrToBitSet(ArrayList integerArray)
  6. invertInPlace(BitSet bs, int n)
  7. isEmptySet(BitSet setToTest)
  8. isHammingDistanceOne(BitSet a, BitSet b)
  9. isSet(int n, BitSet... sets)