微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在表示为字符串的两个二进制数上模拟减法运算符?

如何解决如何在表示为字符串的两个二进制数上模拟减法运算符?

static string ReturnValueIfInputOneIsThirteenAndInputLengthIsEqualWithInputTwoLength(string input,string inputTwo)
        {
            string result = "";
            int temp = 0;
            int inputLength = input.Length - 1;
            int inputTwoLength = inputTwo.Length - 1;
            const int two = 2;

            while (inputLength >= 0 || inputTwoLength >= 0 || temp > 0)
            {
                temp += (inputLength >= 0) ? input[inputLength] - '0' : 0;
                temp += (inputTwoLength >= 0) ? inputTwo[inputTwoLength] - '0' : 0;
                result = (char)(temp % two + '0') + result;
                temp /= two;
                inputLength--;
                inputTwoLength--;
            }

            return result.Trimstart('0');
        }

我必须以某种方式模拟两个以字符串表示的二进制数的减法运算符。所以我要求提供一个建议,在不将它们转换为整数的情况下实现这一点。

例如,如果我输入 101 和 11,结果应该是 10。

这就是我做加法的方式,但我找不到解决方案。

您有什么建议吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。