c# - 如何在表示为字符串的两个二进制数上模拟减法运算符?
问题描述
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。
这就是我加法的方式,但减法我找不到解决方案。
你有什么建议吗?
解决方案
推荐阅读
- python - 如何在 Python 中查找所有可能的字典值组合,这些组合在 Python 中加起来为某个数字,同时仍保留键名
- heroku - 如何使用 Next.js 和 Heroku 管理错误 R14(超出内存配额)?
- sql - 嵌套 where 语句
- ruby-on-rails - 如何使用 ActiveSupport::Notifications 订阅所有查询方法
- javascript - Webpack 5 资产模块与 woff 文件有关
- node.js - 异步/等待的nodejs超时
- reactjs - react.js 和 usemutation 的无限循环
- android - 多线程 runOnUiThread
- json - 在一个混合数据类型的系列中,如何将偶尔出现的列表和字典转换成字符串?
- r - pdfetch 包中的 R pdfetch_boe 错误