Here you can find the source of bitSetToBinaryString(BitSet b, int startBit, int length)
Parameter | Description |
---|---|
b | BitSet to convert |
startBit | Start converting from this bit. Treat it as the least significant bit. |
length | Number of bits to read from the BitSet after the startBit. |
public static String bitSetToBinaryString(BitSet b, int startBit, int length)
//package com.java2s; //License from project: Open Source License import java.util.BitSet; public class Main { /**//from ww w .j a va 2 s . 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(); } }