-2

Example:

a = "100"

b = "11"

Return a + b = “111”.

its done by parsing int but when two strings are more then int size then it will not working.

i tried with long :

long a1=Long.parseLong(a,2);
long b1=Long.parseLong(b,2);
long sum=a1+b1;
String ans=Long.toBinaryString(sum);

is there any methods for double??

Yash Bathia
  • 43
  • 1
  • 13

5 Answers5

2

To exceed the long size you will need BigInteger.

public void test() {
    String a = "100";
    String b = "11";
    BigInteger bA = new BigInteger(a, 2);
    BigInteger bB = new BigInteger(b, 2);
    System.out.println(a + " + " + b + " = " + bA.add(bB).toString(2));
}

This does not help with double though.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

For double you can try this:

double number = 2.2;
Long.toBinaryString(Double.doubleToLongBits(number));
Androidas
  • 169
  • 10
0

https://stackoverflow.com/a/8272792/6049590

Copied and modified from above Source

public double ConvertToDouble(string str){
    long v = 0;
    for (int i = str.Length - 1; i >= 0; i--) v = (v << 1) + (str[i] - '0');
    return = BitConverter.ToDouble(BitConverter.GetBytes(v), 0);
}

https://social.msdn.microsoft.com/Forums/vstudio/en-US/0ff76c9a-8d8c-46f3-94cc-420f719a14e4/how-to-convert-floatdoubleulong-into-binaryotcalhex-string?forum=netfxbcl

Copied and modified from above Source

public string ConvertToString(double value){
    string s = String.Empty;

    foreach (byte b in BitConverter.GetBytes(value))
    {
       s += Convert.ToString(b,2).PadLeft(8,'0'); // for hex. For binary, use 2 and 8. For octal, use 8 and 3
    }
    return s;
}

And now the last one:

double c = ConvertToDouble(a) + ConvertToDouble(b);
string bitString = ConvertToString(c);

string bitString should be your expected result.

Community
  • 1
  • 1
Monarchis
  • 101
  • 6
0

For adding then converting String again :

public String XXX()
{ 
    int a = Double.parseDouble("100.0");
    int b = Double.parseDouble("11.0");

    return (a + b) + "";
}

For adding then returning :

public double XXX()
{ 
    int a = Double.parseDouble("100.0");
    int b = Double.parseDouble("11.0");

    return (a + b);
}

Have a nice day!

Ahmet Batu Orhan
  • 118
  • 2
  • 14
0

If the strings are too long to fit any of the standard types and you don't want to use BigInteger, then you can do it the old-fashioned way using the same algorithm you use when adding two numbers by hand, on paper.

For example, if you have "110" and "11", you would write:

 110
+ 11
----

Then you start from the right and move left, adding the digits. Your first partial result is "1", in the right-hand column:

 110
+ 11
----
   1

Next, "1" and "1" is "0", with a carry of "1". So you write the "0" and note that you have a carry. Your partial result is "01", with the carry.

In the third column you add the "1" and the carry, again giving you "0" with a carry. Your partial result is "001" and the carry. In the fourth column you add the carry, giving you the final result, "1001".

To do that in code, the easiest thing is to pad the shorter number with "0" on the left so that it's the same length as the longer number. So in the example above I would have turned the "11" into "011". Then write a loop that will process the strings from right to left. I'm not a Java programmer, but you should be able to get the idea from this pseudocode.

number1 = "110"
number2 = "011"
result = ""
carry = 0
for i = length(number1) downto 0
    digit1 = number1[i].ToInt()
    digit2 = number2[i].ToInt()
    sum = digit1 + digit2 + carry
    if (sum % 2) == 0
        result = result + "0"
    else
        result = result + "1"
    carry = (result > 1)
end for
if (carry = 1)
    result = result + "1"

That's the slow and simple way to do it. You can use a variation of that method to do it much faster. Basically, you take the bits 32 at a time (from right to left), do the addition, output the resulting 32 bits, maintain the carry (if any), and then move left. You can do that with bytes, words, ints, or longs. Say you're doing it with bytes and your numbers are "010101010101010101010101" and "10110100110101001111". You would split them into 8-bit groups:

    3       2        1
01010101 01010101 01010101
10110100 11010100 11111100

Grab the two bytes from group 1, convert them to int, add them, and output the first 8 bits of the result. If there is a ninth bit to the result (there can't be more than 9 bits), then it's a carry into the next column of numbers.

Continue that until you've finished the last column of numbers. As I said, you can do that with any integral type (byte, short, int, long) to speed up the basic algorithm I laid out for individual bits.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351