Java examples for java.util:BitSet
Convert a BitSet to a binary representation string.
//package com.java2s; import java.util.BitSet; public class Main { /**/*w w w.java 2s .c o m*/ * Convert a BitSet to a binary representation string. * Each bit is converted into "0" or "1" and added to the string. * The result is a binary number with its most significant bit on the left. * * @param b BitSet to convert * @param startBit Start converting from this bit. Treat it as the least significant bit. * @param length Number of bits to read from the BitSet after the startBit. * @return String representation of b. */ public static String bitSetToBinaryString(BitSet b, int startBit, int length) { return bitSetToBinaryString(b, startBit, length, 4); } /** * Convert a BitSet to a binary representation string. * Converting each bit into "0" or "1". * The result is a binary number with its most significant bit on the left. * * @param b BitSet to convert * @param startBit Start converting from this bit. Treat it as the least significant bit. * @param length Number of bits to read from the BitSet after the startBit. * @param wordlength When greater than 0, a space character is added each wordlength number of digits. * @return Binary representation String of the BitSet. */ public static String bitSetToBinaryString(BitSet b, int startBit, int length, int wordlength) { StringBuilder ret = new StringBuilder(); for (int i = length + startBit - 1; i >= startBit; i--) { ret.append(b.get(i) ? "1" : "0"); if (wordlength != -1 && wordlength > 0 && i > 0 && i % wordlength == 0) ret.append(' '); } return ret.toString(); } }