Hi guys, how do i do binary subtraction without using pad left option? I can't seem to figure it out.
I can post my binary addition code if you need to see how i work my codes. Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Nitesh KejriwalRank #253 · 9.9K reputationPosted Sep 4, 2014, 4:58 AM EDT
Beta Tester3 reputationPosted Sep 4, 2014, 5:06 AM EDT
private void button1_Click(object sender, EventArgs e)
{
int temp = 0;
int carry = 0;
string num1 = textBox1.Text;
string num2 = textBox2.Text;
if (num1.Length < num2.Length)
{
for (int i = textBox1.TextLength - 1; i < textBox2.TextLength - 1; i++)
{ num1 = "0" + textBox1.Text; }
}
else if (num1.Length > num2.Length)
{
for (int i = textBox2.TextLength - 1; i < textBox1.TextLength - 1; i++)
{ num2 = "0" + textBox2.Text; }
}
for (int i = num1.Length - 1; i >= 0; i--)
{
temp = int.Parse(num1[i].ToString()) + int.Parse(num2[i].ToString()) + carry;
if (temp == 2)
{
carry = 1;
temp = 0;
textBox3.Text = temp.ToString() + textBox3.Text;
}
else if (temp == 3)
{
carry = 1;
temp = 1;
textBox3.Text = temp.ToString() + textBox3.Text;
}
else
{
carry = 0;
textBox3.Text = temp.ToString() + textBox3.Text;
}
}
if (carry > 0)
{ textBox3.Text = carry.ToString() + textBox3.Text; }
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
// I was told that I can reuse this code if I am to do binary multiplication.
I'm currently self-studying c# gui.