Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8143435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:07:26+00:00 2026-06-06T13:07:26+00:00

I’m working on a small java project aiming to transform a BitSet into several

  • 0

I’m working on a small java project aiming to transform a BitSet into several BitSets and then those into several arrays of Bytes:

For example, I wish to split up in two parts a BitSet and convert each part into an int :

    byte[] bytesToBeConverted = {(byte)0x05, (byte)0x00};
    BitSet bitSetToBeConverted = BitSet.valueOf(bytesToBeConverted);

BitSet BitSetPart1 =new BitSet(8);
BitSetPart1=bitSetToBeConverted.get(0,8);
int intPart1 = (int)(BitSetPart1.toByteArray()[0]); //intPart1 ==5

BitSet BitSetPart2 =new BitSet(8);
BitSetPart2 = bitSetToBeConverted.get(8,16);
int intPart2 = (int)(BitSetPart2.toByteArray()[0]); //intPart2 == 0 is wanted

Whereas no problem does occur in the first part (converting bitSetPart1 into intPart1), the second part, where BitSetpart2 has to be initialized with false, causes an exception to be raised when accessing to the result of the method toByteArray() :java.lang.ArrayIndexOutOfBoundsException
toByteArray seems to return null in that case.

Does that mean that a zero is a forbiden value for that type of operations?
In that case would you rather extend the BitSet Class and Override the toByteArray() method?
or create a class completely separated from the BitSet with an extra method to overcome that problem?

or is there another way to perform that kind of operation that I haven’t mentionned?

Thanks a lot for your answers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:07:27+00:00Added an answer on June 6, 2026 at 1:07 pm

    From the Javadoc of toByteArray():

    More precisely, if

    byte[] bytes = s.toByteArray(); 
    

    then

    bytes.length == (s.length()+7)/8
    

    and from the Javadoc of length():

    Returns the “logical size” of this BitSet: the index of the highest set bit in the BitSet plus one. Returns zero if the BitSet contains no set bits.

    Since the second BitSet contains no set bits, it returns an array of length zero, as the Javadoc clearly specifies.

    If you want to pad the result of toByteArray() out to a specified number of bytes, then use Arrays.copyOf(bitSet.toByteArray(), desiredLength).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.