Java BitSet insertBits(BitSet fromSet, int insertBefore, int count)

Here you can find the source of insertBits(BitSet fromSet, int insertBefore, int count)

Description

Insert a range of bit positions into a BitSet.

License

Open Source License

Parameter

Parameter Description
fromSet The BitSet into which bit positions are to be inserted.
insertBefore The index above which the new bit positions are to be created.
count The number of new bit positions to be created.

Return

A Copy of 'fromSet' with bits in the specified range inserted (in cleared state).

Declaration

public static BitSet insertBits(BitSet fromSet, int insertBefore, int count) 

Method Source Code

//package com.java2s;
/*//w w w .j ava 2  s.  c o m
 * This is a class containing various static utility methods.
 *
 * Copyright (C) 1999 Thomas Studer
 * mailto:tstuder@datacomm.ch
 * http://www.datacomm.ch/tstuder
 *
 * This class is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This class 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this class; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.BitSet;

public class Main {
    /**
     * Insert a range of bit positions into a BitSet.
     *
     * @return A Copy of 'fromSet' with bits in the specified range
     * inserted (in cleared state).
     * @param fromSet The BitSet into which bit positions are to be inserted.
     * @param insertBefore The index above which the new bit positions are to
     * be created.
     * @param count The number of new bit positions to be created.
     */
    public static BitSet insertBits(BitSet fromSet, int insertBefore, int count) {

        BitSet newSet = new BitSet(fromSet.size() + count);

        for (int i = 0; i < insertBefore; i++) {
            if (fromSet.get(i))
                newSet.set(i);
        }

        for (int i = insertBefore; i < fromSet.size(); i++) {
            if (fromSet.get(i))
                newSet.set(i + count);
        }

        return newSet;
    }
}

Related

  1. hammingDistance(BitSet bs1, BitSet bs2)
  2. hasAtLeastOneBitSet(BitSet bitSet, Iterable indexes)
  3. hex2BitSet(byte[] b, int offset, int maxBits)
  4. hexToBits(String s, BitSet ba, int length)
  5. increment(BitSet bits, int size)
  6. intArrToBitSet(ArrayList integerArray)
  7. intToBitSet(int value)
  8. invertInPlace(BitSet bs, int n)
  9. isEmptySet(BitSet setToTest)