3

I am using the BigInteger type from System.Numerics.

For instance:

Dim Mult17 As BigInteger = BigInteger.Parse("1453453452342347")

Questions:

  1. Given a BigInteger, how do I convert it into a BitArray? (Clearly I want the BitArray to hold the binary 10101... representation of the number stored in the BigInteger variable, and I would like to have the option to have it stored in one way or the reverse way.)

  2. I also need to go back from a BitArray to a BigInteger.

Note that there's no sign concern: I am dealing only with positive integers. VB.NET or C# examples will be fine. Thank you.

1 Answer 1

5

Well it looks like you can take a BigInteger to a byte[] using .ToByteArray().

And lucky for you, BitArray takes a byte[] as a constructor parameter.

To go backwards, BitArray to byte[], use this extension, then pass into BigInteger in the constructor

It looks like it should work. Full disclosure, I am too lazy to test this.

3
  • Hmmm, very interesting. A first question though is how the ByteArray relates to the BitArray ? Can i expect to just "streamline" the bits into the bitarray. I have no idea of the internal structure of the biginteger ?
    – Pam
    Dec 19, 2012 at 7:52
  • 1
    @Pam: Why dont you simply TRY it? Hint: A Biginteger 5 .ToArray gives you an array with ONE element and the elements value is 5 ...
    – igrimpe
    Dec 19, 2012 at 7:58
  • Not .ToArray - .ToByteArray. Dec 19, 2012 at 14:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.