本文整理了Java中java.util.BitSet.length()
方法的一些代码示例,展示了BitSet.length()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BitSet.length()
方法的具体详情如下:
包路径:java.util.BitSet
类名称:BitSet
方法名:length
[英]Returns the number of bits up to and including the highest bit set. This is unrelated to the #size of the BitSet.
[中]返回最高位集之前(含最高位集)的位数。这与位集的大小无关。
代码示例来源:origin: redisson/redisson
private static byte[] toByteArrayReverse(BitSet bits) {
byte[] bytes = new byte[bits.length() / 8 + 1];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
final int value = bytes[i / 8] | (1 << (7 - (i % 8)));
bytes[i / 8] = (byte) value;
}
}
return bytes;
}
代码示例来源:origin: redisson/redisson
private static byte[] toByteArrayReverse(BitSet bits) {
byte[] bytes = new byte[bits.length() / 8 + 1];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
final int value = bytes[i / 8] | (1 << (7 - (i % 8)));
bytes[i / 8] = (byte) value;
}
}
return bytes;
}
代码示例来源:origin: org.testng/testng
public int getExitCode() {
int exitCode = 0;
for (int i = 0; i < exitCodeBits.length(); i++) {
if (exitCodeBits.get(i)) {
exitCode = exitCode | (1 << i);
}
}
return exitCode;
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
/** helper for gap encoding. */
private static byte[] bitSetToByteArray(BitSet bitSet) {
while (bitSet.length() % 8 != 0) { bitSet.set(bitSet.length(), true); }
byte[] array = new byte[bitSet.length()/8];
for (int i = 0; i < array.length; i++) {
int offset = i * 8;
int index = 0;
for (int j = 0; j < 8; j++) {
index <<= 1;
if (bitSet.get(offset+j)) { index++; }
}
array[i] = (byte)(index - 128);
}
return array;
}
代码示例来源:origin: apache/hive
private static byte[] toBinary(BitSet bitset) {
byte[] nulls = new byte[1 + (bitset.length() / 8)];
for (int i = 0; i < bitset.length(); i++) {
nulls[i / 8] |= bitset.get(i) ? MASKS[i % 8] : 0;
}
return nulls;
}
代码示例来源:origin: cbeust/testng
public int getExitCode() {
int exitCode = 0;
for (int i = 0; i < exitCodeBits.length(); i++) {
if (exitCodeBits.get(i)) {
exitCode = exitCode | (1 << i);
}
}
return exitCode;
}
}
代码示例来源:origin: graphhopper/graphhopper
private BitSet getValidOn(BitSet validOnDay, int dayShift) {
if (dayShift == 0) {
return validOnDay;
} else {
BitSet bitSet = new BitSet(validOnDay.length() + 1);
for (int i=0; i<validOnDay.length(); i++) {
if (validOnDay.get(i)) {
bitSet.set(i+1);
}
}
return bitSet;
}
}
代码示例来源:origin: apache/kylin
@Override
public BitSet copy(final Kryo kryo, final BitSet original) {
final BitSet result = new BitSet();
final int length = original.length();
for (int i = 0; i < length; i++) {
result.set(i, original.get(i));
}
return result;
}
代码示例来源:origin: com.h2database/h2
/**
* Get the fill rate of the space in percent. The value 0 means the space is
* completely free, and 100 means it is completely full.
*
* @return the fill rate (0 - 100)
*/
public int getFillRate() {
int total = set.length(), count = 0;
for (int i = 0; i < total; i++) {
if (set.get(i)) {
count++;
}
}
if (count == 0) {
return 0;
}
return Math.max(1, (int) (100L * count / total));
}
代码示例来源:origin: Sable/soot
public List<N> getDominators(N node) {
// reconstruct list of dominators from bitset
List<N> result = new ArrayList<N>();
BitSet bitSet = nodeToFlowSet.get(node);
for (int i = 0; i < bitSet.length(); i++) {
if (bitSet.get(i)) {
result.add(indexToNode.get(i));
}
}
return result;
}
代码示例来源:origin: alipay/sofa-bolt
/**
* from bit set to byte
* @param bs
* @return byte represent the bit set
*/
public static byte toByte(BitSet bs) {
int value = 0;
for (int i = 0; i < bs.length(); ++i) {
if (bs.get(i)) {
value += 1 << i;
}
}
if (bs.length() > 7) {
throw new IllegalArgumentException("The byte value " + value
+ " generated according to bit set " + bs
+ " is out of range, should be limited between ["
+ Byte.MIN_VALUE + "] to [" + Byte.MAX_VALUE + "]");
}
return (byte) value;
}
代码示例来源:origin: ethereum/ethereumj
private int getFirstBitSet(byte[] bytes) {
BitSet b = BitSet.valueOf(bytes);
for (int i = 0; i < b.length(); i++) {
if (b.get(i)) {
return i;
}
}
return 0;
}
代码示例来源:origin: stackoverflow.com
public class Bits {
public static BitSet convert(long value) {
BitSet bits = new BitSet();
int index = 0;
while (value != 0L) {
if (value % 2L != 0) {
bits.set(index);
}
++index;
value = value >>> 1;
}
return bits;
}
public static long convert(BitSet bits) {
long value = 0L;
for (int i = 0; i < bits.length(); ++i) {
value += bits.get(i) ? (1L << i) : 0L;
}
return value;
}
}
代码示例来源:origin: GlowstoneMC/Glowstone
private int findNewSectorStart(int sectorsNeeded) {
int start = -1;
int runLength = 0;
for (int i = sectorsUsed.nextClearBit(0); i < sectorsUsed.length(); i++) {
if (sectorsUsed.get(i)) {
// must reset
start = -1;
runLength = 0;
} else {
if (start == -1) {
start = i;
}
runLength++;
if (runLength >= sectorsNeeded) {
return start;
}
}
}
// reached the end, append to the end of the region instead
return -1;
}
代码示例来源:origin: opentripplanner/OpenTripPlanner
@Override public void serialize(BitSet bitSet, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartArray();
for (int i = 0; i < bitSet.length(); i++) {
jsonGenerator.writeBoolean(bitSet.get(i));
}
jsonGenerator.writeEndArray();
}
}
代码示例来源:origin: org.codehaus.groovy/groovy
/**
* Support the subscript operator for a Bitset
*
* @param self a BitSet
* @param index index to retrieve
* @return value of the bit at the given index
* @see java.util.BitSet
* @since 1.5.0
*/
public static boolean getAt(BitSet self, int index) {
int i = normaliseIndex(index, self.length());
return self.get(i);
}
代码示例来源:origin: com.thoughtworks.xstream/xstream
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
BitSet bitSet = (BitSet) source;
StringBuffer buffer = new StringBuffer();
boolean seenFirst = false;
for (int i = 0; i < bitSet.length(); i++) {
if (bitSet.get(i)) {
if (seenFirst) {
buffer.append(',');
} else {
seenFirst = true;
}
buffer.append(i);
}
}
writer.setValue(buffer.toString());
}
代码示例来源:origin: apache/ignite
/**
* Checks that given {@code nodesSet} forms fully-connected component.
*
* @param nodesSet Set of cluster nodes.
* @return {@code True} if all given cluster nodes are able to connect to each other.
*/
public boolean checkFullyConnected(BitSet nodesSet) {
int maxIdx = nodesSet.length();
Iterator<Integer> it = new BitSetIterator(nodesSet);
while (it.hasNext()) {
int idx = it.next();
for (int i = 0; i < maxIdx; i++) {
if (i == idx)
continue;
if (nodesSet.get(i) && !connections[idx].get(i))
return false;
}
}
return true;
}
}
代码示例来源:origin: osmandapp/Osmand
public void printRule(PrintStream out) {
out.print(" Select " + selectValue + " if ");
for(int k = 0; k < filterTypes.length(); k++) {
if(filterTypes.get(k)) {
String key = universalRulesById.get(k);
out.print(key + " ");
if(filterNotTypes.length() > 0) {
out.print(" ifnot ");
for(int k = 0; k < filterNotTypes.length(); k++) {
if(filterNotTypes.get(k)) {
String key = universalRulesById.get(k);
out.print(key + " ");
代码示例来源:origin: apache/kylin
@Override
public void write(final Kryo kryo, final Output output, final BitSet bitSet) {
final int len = bitSet.length();
output.writeInt(len, true);
for (int i = 0; i < len; i++) {
output.writeBoolean(bitSet.get(i));
}
}
内容来源于网络,如有侵权,请联系作者删除!