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?

Thanks in advance.

[571 byte] By [gonzalo_ron@yahoo.coma] at [2007-11-27 11:19:05]
# 1

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....

petes1234a at 2007-7-29 14:34:44 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for your answer!,

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

Thanks a lot!.

gonzalo_ron@yahoo.coma at 2007-7-29 14:34:44 > top of Java-index,Java Essentials,Java Programming...
# 3

Why are you using BitInteger for Bit Storage?

Why not just use a Byte Array?

TuringPesta at 2007-7-29 14:34:44 > top of Java-index,Java Essentials,Java Programming...