Get all Indexes of the set bits in a BitSet

java bitset
bitset to int java
java bitset to string
bitset nextclearbit
java bitset from byte array
java bitset performance
java bitset equals
bitset valueof

I'm looking for a fast algorithm with gives me all the indexes of the set bits in a BitSet object. This is slow:

BitSet bitSet = ...
Collection<Integer> indexes = new ArrayList<Integer>(bitSet.cardinality());
int nextSetBit = bitSet.nextSetBit(0);
for (int i = 0; i < bitSet.cardinality(); ++i ) {
    indexes.add(nextSetBit);
    nextSetBit = bitSet.nextSetBit(nextSetBit + 1);
}
...

Any help is appreciated!

No need to use bitSet.cardinality() at all:

for (int i = bitSet.nextSetBit(0); i != -1; i = bitSet.nextSetBit(i + 1)) {
    indexes.add(i);
}

java - Get all Indexes of the set bits in a BitSet, No need to use bitSet.cardinality() at all: for (int i = bitSet.nextSetBit(0); i != -1; i = bitSet.nextSetBit(i + 1)) { indexes.add(i); }. Test if all bits are set. Returns whether all of the bits in the bitsetare set (to one). For this function to return true, all bits up to the bitset sizeshall be set. Parameters. none. Return value. trueif all of the bits in the bitsetare set (to one), and falseotherwise. Example. 1.

As specified in BitSet#nextSetBit(int) javadocs :

//To iterate over the true bits in a BitSet, use the following loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
     // operate on index i here
     if (i == Integer.MAX_VALUE) {
         break; // or (i+1) would overflow
     }
 }

Find position of the only set bit, C++ program to find position of only set bit in a given number. #include <bits/stdc​++.h>. using namespace std;. // A utility function to check whether n is a power  For every index for which this BitSet contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.

As of Java 8 you can use BitSet's stream() method.

IntStream stream()

Returns a stream of indices for which this BitSet contains a bit in the set state.

Core Java : a Comprehensive Study, When a bit set is first constructed, all the bits in it have the value false. Every bit The length of the bit set is the index of the highest set bit in the bit set plus one. For every index for which this BitSet contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.

Change the loop (you increase the complexity to O(N^2) because you call cardinality() in each loop iteration):

for (int e = bitSet.cardinality(), i = 0; i < e; ++i ) {
    indexes.add(nextSetBit);
    nextSetBit = bitSet.nextSetBit(nextSetBit + 1);
}

BitSet nextSetBit() method in Java, This method in BitSet Class is used to return the index of the first bit that is set to true, that Note : To iterate over the true bits in a BitSet, use the following loop: The beauty of this solution is the number of times it loops is equal to the number of set bits in a given integer. 1 Initialize count: = 0 2 If integer n is not zero (a) Do bitwise & with (n-1) and assign the value back to n n: = n& (n-1) (b) Increment count by 1 (c) go to step 2 3 Else return count.

Java in a Nutshell, The length() method returns one more than the highest index of a set bit (i.e., a true value). This means that a BitSet that contains all false values will have a  Algorithm: (Example 12(1100)) Let I/P be 12 (1100) 1.Take two's complement of the given no as all bits are reverted except the first '1' from right to left (0111) 2 Do a bit-wise & with original no, this will return no with the required one only (0100) 3 Take the log2 of the no, you will get (position - 1) (2) 4 Add 1 (3)

BitSet (Java Platform SE 8 ), By default, all bits in the set initially have the value false . Every bit Returns the "logical size" of this BitSet : the index of the highest set bit in the BitSet plus one. Iterating over set bits quickly (SIMD edition) Suppose that you have a long sequence of bits 10101011100000 … you want to visit all the bits set to 1. That is, given 10101011100000 , you would like to get the indexes of all the bits set to one: 0,2,4,6,7,8,9.

BitSet (Java SE 13 & JDK 13 ), By default, all bits in the set initially have the value false . Every bit Returns the "logical size" of this BitSet : the index of the highest set bit in the BitSet plus one. Returns an index of a next bit which value is false after startIndex (inclusive). Returns size if there is no such bits between startIndex and size - 1 assuming that the set has an infinite sequence of false bits after (size - 1)-th.

Comments
  • Please read this nice article java-performance.info/bit-sets
  • @myborobudur If it didn't help your perceived slowness most likely is not caused by the BitSet (either its so small it doesn't matter or the autoboxing to Integer overshadows it by far). Use a profiler to measure.
  • It is better to use the solution that does not call cardinality()