3 Replies Latest reply: Oct 12, 2006 1:56 AM by 807607 RSS

    Problem with BitSet conversion to 32 bit Integer

      I have the following problem with using the BitSet class:

      I have group of 8 bit sets each having varying number of bits say {8,8,1,2,6,1,1,2,1,2} and the bits in them are respectively given in binary below:

      { 00000000, 00000000, 0, 00, 000000, 0, 0, 00, 0, 00 }

      I need to convert these bit set into a single bit set and then later produce an integer from the bit set which is a 32 bit integer. is there any way that we could do a conversion from bit set to a 32 bit integer. Is there a solution for this interms of implementation.
        • 1. Re: Problem with BitSet conversion to 32 bit Integer
          Lots of left-shifting, I would say.
          • 2. Re: Problem with BitSet conversion to 32 bit Integer
            I did the following steps, i took out the last bit set as Val, i did this shift and ORing but it wouldnt return the desired value as a 32 bit integer but i just get a single value say as "C" [where C is the value of one of the bitset] when i use outPutBig.toString(16); I then learnt from one of my colleagues that BigInteger was immutable and thats the result its not able to produce a 32 bit integer as a result of shifts and Oring. Is there any thing that needs to be modified to this code to include the 32-bit conversion.
            private String getHexStringFromRegister() {
                   //m_Bitbtn.length is the number of bit sets
                    BitSet outPutValue = m_Bitbtn[m_Bitbtn.length-1].getM_Bits();
                   //convert this bit set to a byte array
                    byte[] outPutValByte = BitSetUtilities.toByteArray(outPutValue);
                  //convert to a big integer
                    BigInteger outPutBig = new BigInteger(outPutValByte);
                    BigInteger inPutBig;
                    byte[] inputValByte;
                   //m_Bitbtn.length is the number of bit sets
                    for(int i=m_Bitbtn.length-2;i>0;i--){
                       //m_Bitbtn.getBits() will return the number of bits in the bit set "i"
            inputValByte = BitSetUtilities.toByteArray(m_Bitbtn[i].getBits());

            //construct a big integer
                        inPutBig = new BigInteger(inputValByte);
                        //left shift over the higher order bits to make room for bit field i

            //OR the register bit value from the bit field with outputBig integer
            outPutBig = outPutBig.or(inPutBig);

            System.out.println("outputString.toString()" +outPutBig.toString());          

            return outPutBig.toString();

            • 3. Re: Problem with BitSet conversion to 32 bit Integer
              Is that...hungarian notation?


              Since bitset grows dynamically like a vector, you can just keep adding the other bitsets to the first one.

              I.e. in pseudocode
              for(int i = 0;i < bs.length;i++)
                  for(int j = 0;j < bs.length(); j++)
              b.set(someIndex, bs[i].get(j));