Java Utility Library

Java BitSet - toLongArray() Method



The java.util.BitSet.toLongArray() method returns a new long array containing all the bits in the given BitSet.

More precisely, if

long[] longs = s.toLongArray();
then, longs.length == (s.length()+63)/64 and
s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)
for all n < 64 * longs.length
Where s is the given BitSet

Syntax

public long[] toLongArray()

Parameters

No parameter is required.

Return Value

Returns a long array containing all the bits in the given BitSet.

Exception

NA.

Example:

In the example below, the java.util.BitSet.toLongArray() method returns a long array containing all the bits in the given BitSet called BSet.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a BitSet
    BitSet BSet = new BitSet();

    //populating BitSet
    BSet.set(10);
    BSet.set(20);
    BSet.set(30);
    BSet.set(40);
    BSet.set(50); 

    //printing BitSet
    System.out.println("BSet contains: " + BSet);
    
    //convert the BitSet into a long array
    long[] Arr = BSet.toLongArray();

    //print the long array
    System.out.print("Arr contains: ");
    for(long i: Arr)
      System.out.print(i + " ");
  }
}

The output of the above code will be:

BSet contains: {10, 20, 30, 40, 50}
Arr contains: 1127000493261824 

❮ Java.util - BitSet