8

I have two strings:

string a = "00001"; /* which is decimal 1 I've converted with next string: string a = Convert.ToString(2, 2).PadLeft(5, '0'); */ string b = "00010"; 

I want to perform binary addition between the two so the answer will be 00011 ( 3).

4
  • 4
    Homework it might be, but still quite an interesting one - it piqued my curiosity enough that I've got a Console app open and fiddling around with it right now! ;) Commented Feb 12, 2010 at 15:34
  • 1
    not really homework >.< project :D Commented Feb 12, 2010 at 16:10
  • Actually, looking at your comments on the accepted answer I think you might be looking in the wrong direction. You're not really looking at addition I think, but at a bitwise AND. Commented Feb 17, 2010 at 15:53
  • @Erik i was looking in the wrong direction true but thanks to this answer i now made a new function that takes 2 integers and returns a string equivalent with the proceeding zero's , which is exactly what i needed. Commented Feb 17, 2010 at 17:05

8 Answers 8

21

System.Convert should be able to do the work for you

int number_one = Convert.ToInt32(a, 2); int number_two = Convert.ToInt32(b, 2); return Convert.ToString(number_one + number_two, 2); 

(you may have to tune the strings a bit)

Sign up to request clarification or add additional context in comments.

3 Comments

string a = "00001"; string b = "00011"; int num1 = Convert.ToInt32(a, 2); int num2 = Convert.ToInt32(b, 2); string ans = Convert.ToString(num1 + num2, 2); MessageBox.Show(ans); "thanks alot :) u have saved my project !!!!!!!!!!!!!!!!"
i needed to caluclate attendance using this logic :) , as 00001 wld represent absent for 1st hour ( we haf hourly wise attendance) so if a student is absent for the next hr in the same day 00001 - absent first hr 00010 - absent second hr ------ 00011 - absent for 1st and second hr :D it works !! thank u
I have to say, I didn't know that particular override of Convert.ToInt32 exists!
8

You do it just as you would do it on paper. Start from right and move left. if A[i] + B[i] + carry >= 2, carry remains 1 and you move on. Otherwise, write A[i] + B[i] + carry and set carry to 0.

a = "00001"; b = "00010";

carry = 0; a[4] + b[4] + carry = 1, write 1, set carry = 0: 00001

a[3] + b[3] + carry = 1, write 1, set carry = 0: 00011

And so on.

Comments

2
private static bool[] BinaryAdd(bool[] originalbits, long valuetoadd) { bool[] returnbits = new bool[originalbits.Length]; for (long i = 0; i <= valuetoadd - 1; i++) { bool r = false; //r=0 for (long j=originalbits.Length-1;j<=originalbits.Length;j--) { bool breakcond = false; bool o1 = originalbits[j]; if (r == false) { if (o1 == false) { o1 = true; breakcond = true; }//break else if (o1 == true) { o1 = false; r = true; } } else { if (o1 == false) { o1 = true; breakcond = true; }//break else if (o1 == true) { o1 = false; r = true; } } originalbits[j] = o1; if (breakcond == true) { break; } } } returnbits = originalbits; return returnbits; } 

1 Comment

You should add some explanation to your example so novices can understand what it does.
1
public static string AddBinary(string a, string b) { string result = ""; int s = 0; int i = a.Length - 1, j = b.Length - 1; while (i >= 0 || j >= 0 || s == 1) { s += ((i >= 0) ? a[i] - '0' : 0); s += ((j >= 0) ? b[j] - '0' : 0); result = (char)(s % 2 + '0') + result; s /= 2; i--; j--; } return result; } 

Just it. Extra Link.

Comments

0

Very easy -- write a lookup table for 'addition' of binary characters, don't forget to carry if necessary, and send me 50% of the credit you get for the work.

Comments

0
var sum = Convert.ToString(Convert.ToInt32("00010", 2) + Convert.ToInt32("00001", 2), 2).PadLeft(5, '0'); 

"00011"

PadLeft is not really needed, but if the strings had different length you would get a the same format. Example:

var sum = Convert.ToString(Convert.ToInt32("0000010", 2) + Convert.ToInt32("001", 2), 2).PadLeft(5, '0'); 

"00011"

Comments

-1

I would recommend parsing the data to ints and then adding them, then outputing the result as binary.

Comments

-1
If a[4] + b[4] + carry = 3 then write 1, carry 1 Else If a[4] + b[4] + carry = 2 then write 0, carry 1 Else If a[4] + b[4] + carry = 1 then write 1, carry 0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.