Java BitSet binaryStringToBitSet(String stringRepresentation)

Here you can find the source of binaryStringToBitSet(String stringRepresentation)

Description

Convert the string representation to a bit set

License

Open Source License

Parameter

Parameter Description
stringRepresentation the string representation

Return

the bit set

Declaration

public static BitSet binaryStringToBitSet(String stringRepresentation) 

Method Source Code

//package com.java2s;
/*//from w w w . j ava 2s  . c o m
 * Copyright (c) 2010 The Jackson Laboratory
 * 
 * This 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.
 *
 * This software 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 this software.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.BitSet;

public class Main {
    /**
     * Convert the string representation to a bit set
     * @param stringRepresentation
     *          the string representation
     * @return
     *          the bit set
     */
    public static BitSet binaryStringToBitSet(String stringRepresentation) {
        BitSet bitSet = new BitSet(stringRepresentation.length());
        for (int i = 0; i < stringRepresentation.length(); i++) {
            if (stringRepresentation.charAt(i) == '1') {
                bitSet.set(i);
            } else if (stringRepresentation.charAt(i) != '0') {
                throw new IllegalArgumentException("bad format: " + stringRepresentation);
            }
        }
        return bitSet;
    }
}

Related

  1. asArrayWith0s(int max, BitSet bits)
  2. binaryToGray(BitSet binary)
  3. bitsetCopy(BitSet src, int srcOffset, BitSet dest, int destOffset, int length)
  4. bitSetTest(BitSet bitSet, int index)
  5. BitString2BitSet(String string)