0

I have two string namely string1 and string2. they contain binary numbers and I want to manipulate them with different kinds of operations.

for example:

      string string1 = "1000";
      string string2 = "1011";

how can I Add/Subtract string1 and string2 so that:

      AnswerToAddtion = 10011               //1000 + 1011
      AnswerToSubtratction = 1101           //1000-1011
user1548960
  • 357
  • 3
  • 8
  • 16

1 Answers1

5

You can use Convert.ToInt32("11011",2); to parse your string to an integer value.

To convert an integer to a binary string, you can use Convert.ToString(myInt, 2);

Josh C.
  • 4,303
  • 5
  • 30
  • 51
  • @ChristopherRayl do you want the result of addition/subtraction in a string? The OP states they want to manipulate the values with different operations, and the problem is the data type is string. I think the solution is to use a more suiting data type. – Josh C. Dec 04 '12 at 15:15
  • I think he just wants to convert a binary string into a binary number so that he can perform binary arithmetic on it. – Christopher Rayl Dec 04 '12 at 15:17
  • @ChristopherRayl what difference should it make to do binary operations on an int32? What data type is a binary type? Int32 should be sufficient. If a binary string result is desired, generating the string is simple enough. – Josh C. Dec 04 '12 at 15:19
  • I think (s)he's looking for something like: http://stackoverflow.com/a/2252904/930878 – Christopher Rayl Dec 04 '12 at 15:23