4 Replies Latest reply: Oct 16, 2007 3:35 PM by 807605

# Bits to BigInteger

Hi friends!,

I want to convert a bit set in a BigInteger. I have tried with a byte array but I have the following problem:

BigInteger a = new BigInteger(new byte[]{127});

!That is good!, because when I print my BigInteger the value is 127.

BigInteger a = new BigInteger(new byte[]{1,127});

That is wrong because my BigInteger print 383, that is equivalent to 101111111. The BigInteger put the sign bit (from 127) in my BigInteger.

I was waiting 255. That is equivalent to 11111111

Can you help me?

• ###### 1. Re: Bits to BigInteger
I think that java doesn't have unsigned bytes, just signed bytes, and as such, I believe that the compiler's conversions are correct.

I think I'm missing info to fully proces what you are trying to do. What is the source of this (these) byte array(s)? Can you instead use integers? hexadecimal notation (i.e., 0xff)? I guess I need to know your constraints and desires....
• ###### 2. Re: Bits to BigInteger

I was trying to do a genetic algorithm, but my problem was finishing.

Thanks a lot!.
• ###### 3. Re: Bits to BigInteger
Why are you using BitInteger for Bit Storage?
Why not just use a Byte Array?
• ###### 4. Re: Bits to BigInteger
Hi!,

The reason is I am working with genetic algorithm and I need only the 0 and 1 values. The bytes array it's not natural for my application.

Thanks a lot.