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).

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
Alfred
  • 257
  • 2
  • 5
  • 14
  • 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! ;) – Rob Feb 12 '10 at 15:34
  • 1
    not really homework >.< project :D – Alfred Feb 12 '10 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. – Erik van Brakel Feb 17 '10 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. – Alfred Feb 17 '10 at 17:05

8 Answers8

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)

Benoît Vidis
  • 3,908
  • 2
  • 23
  • 24
  • 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 !!!!!!!!!!!!!!!!" – Alfred Feb 12 '10 at 16:05
  • 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 – Alfred Feb 12 '10 at 16:06
  • I have to say, I didn't know that particular override of Convert.ToInt32 exists! – Rob Feb 13 '10 at 11:30
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.

IVlad
  • 43,099
  • 13
  • 111
  • 179
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
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.

ihsan güç
  • 241
  • 3
  • 7
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.

user229044
  • 232,980
  • 40
  • 330
  • 338
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
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"

krazor
  • 36
  • 5
-1

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

C. Ross
  • 31,137
  • 42
  • 147
  • 238
-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
Jeffrey L. Roberts
  • 2,844
  • 5
  • 34
  • 69