How would I do 10101base2 + 111base2 =? I already know how to convert the base to another base. But how would I do them with addition?
Asked
Active
Viewed 826 times
0
-
1Convert each separately then add them. – Sotirios Delimanolis Sep 26 '13 at 01:55
-
Numbers are numbers - doesn't matter what you're doing with them. – Jonathon Reinhart Sep 26 '13 at 01:57
-
Why would you want to add another step for yourself and convert to decimal? Just add them as binary numbers (I'm assuming your end result will be binary as well) – Josh M Sep 26 '13 at 02:01
2 Answers
2
I find java's BigInteger to be the best among them all for bit manipulation. Among its wide usages (mainly with storing huge numbers and for its wide array of supported operations), you do have the option for base conversion from 2 to 36. As for addition of these binary digits, you can use BigInteger.add(BigIntger)
function provide by them.
Example :
BigInteger num_1=new BigInteger("10101",2); //Store as Binary
BigInteger num_2=new BigInteger("111",2); //Store as Binary
BigInteger result=num_1.add(num_2);
//Display the result using BigInteger.toString(int base)
System.out.println("Result = "+result.toString(10)); //Showing result in Decimal base here
Ofcourse, if its having fractional digits, the method described by William Gaul is to be used.

Community
- 1
- 1

Kaushik NP
- 6,733
- 9
- 31
- 60
1
int result = 0b10101 + 0b111;
Or if your inputs are strings:
int result = Integer.parseInt("10101", 2) + Integer.parseInt("111", 2);
EDIT: If you're asking how to view the result in binary form, there's also this:
System.out.println(Integer.toBinaryString(result));

William Gaul
- 3,181
- 2
- 15
- 21