2

I have numbers as 10101, 1000, 11101,.... and so on. I want to store these numbers using bitset class, but dont know how to do that? Please help

1
  • Bitset class? What do you mean? Commented Jun 14, 2012 at 8:47

3 Answers 3

1

You should use byte array instead.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a BitSet implementation in the standard Java SE API, you should try using that one.

Comments

1

If you want to use a binary number representation to create a BitSet, this can be done in two steps:

e.g.

BigInteger a = new BigInteger("10101", 2); //base 2 for binary
BitSet aBits = BitSet.valueOf(a.toByteArray());

BitSet.valueOf() is new in Java 7 so won't work in earlier Java versions.

Edit:

If you don't like initializing BigInteger with strings and your binary numbers fit into 64-bits, you could also use Java 7's binary literals and initialize the BigInteger using BigInteger.valueOf(long) method instead.

1 Comment

the method u specified above would work for 10101, but this is just 1 number. I have around 100 such numbers, how do I deal with them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.