My andgate works when I use just 1 bit strings, but anything else, it doesn’t work at all and tells me “Specific argument was out of the range of valid values. Parameter name: startIndex.”
Anyone know what I’m doing wrong here? And is there a better way to add to the end of a string? Thanks!
private string parsestrings(string s1, string s2)
{
int n = s1.Length;
int m = s2.Length;
int l;
string s = "";
if (n > m)
{
for(int i = 0; i <= n; i++)
{
l = AndGate(s1[i], s2[i]);
s.Insert(i, IntToBinary(l));
}
}
else
{
for (int i = 0; i <= n; i++)
{
l = AndGate(s1[i], s2[i]);
s.Insert(i, IntToBinary(l));
}
}
return s;
}
private int AndGate(int m, int n)
{
if (m == 1 && n == 1)
return 1;
if (m == 1 && n == 0)
return 0;
if (m == 0 && n == 1)
return 0;
else
return 0;
}
Your logic seems a little off. If
n > m, then the loop should probably stop atminstead ofn:The second loop seems right, though.
A second thought is that the condition
i <= mshould probably bei < m, sinces1[m]is one character past the string.Finally, you can either follow spender’s advice, or use a
StringBuilderinstead. It’s generally better for constructing a string than continually adding to and updating a string.