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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Beta TesterPosted Sep 4, 2014, 9:06 AM
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.
Nitesh KejriwalPosted Sep 4, 2014, 8:58 AM