BitSet toLongArray() Method in Java with Examples

The java.util.BitSet.toLongArray() is an inbuilt method of BitSet class that is used to produce a new long array containing all of the bits of the existing BitSet. As per the official documentation, this process works in the following way:

if, long[] longs = bit_set.toLongArray();
then, longs.length == (bit_set.length()+63)/64 and,
bit_set.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0) for all n < 64 * longs.length

Syntax:

Bit_Set.toLongArray()

Parameter: The method does not take any parameters.

Return value: The method returns the set consisting of LongArray representation of the elements of the given BitSet.

Below programs illustrate the working of java.util.BitSet.toLongArray() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toLongArray()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(10);
        init_bitset.set(20);
        init_bitset.set(30);
        init_bitset.set(40);
        init_bitset.set(50);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        long[] arr = init_bitset.toLongArray();
        System.out.println("The LongArray is: " + arr);
  
        // Displaying the byteArray
        System.out.println("The elements in the LongArray:");
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + ", ");
    }
}

chevron_right


Output:

BitSet: {10, 20, 30, 40, 50}
The LongArray is: [J@232204a1
The elements in the LongArray:
1127000493261824,

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate toLongArray()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(48);
        init_bitset.set(64);
        init_bitset.set(15);
        init_bitset.set(95);
        init_bitset.set(105);
        init_bitset.set(21);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        long[] arr = init_bitset.toLongArray();
        System.out.println("The LongArray is: " + arr);
  
        // Displaying the byteArray
        System.out.println("The elements in the LongArray:");
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + ", ");
    }
}

chevron_right


Output:

BitSet: {15, 21, 48, 64, 95, 105}
The LongArray is: [J@232204a1
The elements in the LongArray:
281474978840576, 2201170739201,


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.