相关文章推荐
近视的香菜  ·  go - Golang json ...·  2 年前    · 
小胡子的大葱  ·  iText7 ...·  2 年前    · 
威武的香瓜  ·  Android studio 中使用svn ...·  2 年前    · 

Java BitSet toLongArray() method

The toLongArray() method of Java BitSet class returns a new long array which contains all the bits of this bit set.

This method works on the basis of following algorithm:

long[] longs = bitset.toLongArray(); then longs.length == (bitset.length()+63)/64 and bitset.get(n) == ((longs[n/64] & (1L<

for all n < 64 * longs.length.

Syntax:

public long[] toLongArray()

Parameter:

Returns:

This method returns a long array which contains all the bits of this bit set.

Exception:

Compatibility Version:

Java 1.7 and above

Example 1

import java.util.BitSet; public class BitSetToLongArrayExample1 { public static void main(String[] args) { BitSet bitset = new BitSet(); bitset.set(0); bitset.set(1); bitset.set(3); bitset.set(5); bitset.set(6); System.out.println("bitset: "+bitset); long[] lngArray = bitset.toLongArray(); System.out.println("byte array: "+lngArray[0]); Test it Now

Output:

bitset: {0, 1, 3, 5, 6}
byte array: 107

Example 2

Each long array of the BitSet is set at the interval of each long bit space (64 bit). The length of a long bit space increases if the set bit is set above the previous long size.

In this example, the set bits from index 0 to 9 are placed in the first long bit space and the set bit from index 64 and 65 are placed at second long bit space.

import java.util.BitSet; public class BitSetToLongArrayExample2 { public static void main(String[] args) { BitSet bitset = new BitSet(); bitset.set(0); bitset.set(1); bitset.set(3); bitset.set(5); bitset.set(6); bitset.set(8); bitset.set(9); bitset.set(64); bitset.set(65); System.out.println("bitset: "+bitset); long[] lngArray = bitset.toLongArray(); for(int i=0;i
Test it Now

Output:

bitset: {0, 1, 3, 5, 6, 8, 9, 64, 65}
byte array1 to long: 875
byte array2 to long: 3
Next TopicBitSet toString() Method

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Digital Marketing
  • On Page and Off Page SEO
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry
  • Training For College Campus

    JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
    Duration: 1 week to 2 week

    Learn Tutorials

    Learn Java TutorialLearn C TutorialLearn C++ TutorialLearn C# TutorialLearn PHP TutorialLearn HTML TutorialLearn CSS TutorialLearn JavaScript TutorialLearn jQuery TutorialLearn Spring Tutorial

    Our Websites

    Javatpoint.com Hindi100.com Lyricsia.com Quoteperson.com Wordcountee.com Charactercountee.com Jobandplacement.com

    Our Services

    Website Development

    Android Development

    Website Designing

    Digital Marketing

    Summer Training

    Industrial Training

    College Campus Training

    Contact

    Address: G-13, 2nd Floor, Sec-3

    Noida, UP, 201301, India

    Contact No: 0120-4256464, 9990449935

    Contact Us Subscribe Us Privacy Policy Sitemap