I've been trying to add two long strings of binaries using the Integer.parseInt method as radix 2. However, I don't believe that this works for long strings of binary numbers. Is there a better way to do this?
2 Answers
You can use BigInteger with radix:
BigInteger decInt = new BigInteger("111111111111111111111111111111111001111",2);
Have a look at the documentation: http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int)
Comments
You can use BigInteger for this. The BigInteger(byte[]) constructor takes a byte array that's interpreted as a two's complement integer, and you can then use the add function to add the two. Keep in mind that add doesn't modify either of the objects, as BigIntegers are immutable. Instead, it returns a new object.
byte[]s that represent the numbers you want to add?Integer.parseIntwith radix 2 is the binary ofInteger.MAX_VALUE