【问题标题】:Long binary string arithmetic in javajava中的长二进制字符串算术
【发布时间】:2014-01-16 15:58:57
【问题描述】:

我正在做一个基于服务器的程序,我想将 2 个二进制字符串相乘。这些字符串很长,因此它们不能转换为longint 类型。这些字符串之一的示例是:

01100010 00110110 00110011 00110111 00110100 00111001 00111001 00111001 00110110 01100011 00110110 01100101 00111001 00110011 00110011 00110010 00110010 00110010 01100001 00110101 01100100 01100011 01100011 01100010 01100100 00111000 01100100 01100100 00110010 00110110 00110110 00110100 00111000 01100110 00110001 00110100 00110110 01100110 01100110 01100100 00110100 00110101 00110100 01100010 01100010 00111001 00111001 00110110 01100110 01100010 00111000 00110011 00110000 00110011 00110010 01100110 01100010 00110001 01100010 01100001 00110100 01100011 01100011 00111000 00110110 00110111 01100110 00110001 00111001 00110110 00110110 00110001 00110001 01100101 00110010 00111000 01100100 01100110 01100110 01100001 01100100 00110100 00110110 00110000 00110010 00111001 00111001 00110011 00111000 01100001 00111001 00110111 00110111 00110011 00110010 01100011 00110100 00110000 01100011 01100101 01100010 01100011 01100011 00110101 01100110 00110111 00110000 00110110 00110000 00110110 00110101 01100101 01100001 01100100 00110011 01100100 00110100 01100110 01100110 00110111 00110110 00110011 00110111 00110100 00111001 00111001 00111001 00110000 00110001 00111001 01100001 01100010 01100101 00110011 00110010 00111001 01100011 01100110 01100101 00110011 00110010 01100011 01100011 00110010 00111000 00110110 00110001 01100001 00110111 00110110 00110101 01100010

这个字符串可以有空格,也可以没有。没关系。我的问题是,我如何将 01100011 与这个字符串相乘?乘数的长度是可变的,所以灵活性是必须的。

提前致谢!

【问题讨论】:

  • 你能用BigInteger吗?
  • 我可以,但我不确定如何用它进行二进制乘法...因为我从未使用过 biginteger...
  • call multiply() 数字是数字,无论它们显示在什么基础上
  • BigInteger 有一个接受字符串和一个基数的构造函数,一个接受基数参数的 toString 方法,以及一个 multiply 方法。

标签: java string math binary


【解决方案1】:

只需使用BigInteger 并指定基数:

BigInteger bigNumber = new BigInteger("101000101110...1010".replaceAll("\s+", ""), 2);

你会发现它支持multiplyadd等算术运算。例如,在上面创建变量之后,我们可以说:

BigInteger bigProduct = bigNumber.multiply(someOtherBigNumber);

假设我们创建了另一个BigInteger

编辑:正如 cmets 中所指出的,您需要删除二进制字符串表示中的空格,这很容易做到 - 我已经更新了我的答案以包含这个一步。

【讨论】:

  • OP 必须删除空格。
  • new BigInteger("00110001 01100001 00110111 00110110 00110101 01100010".replace(" ",""),2)
【解决方案2】:

最简单的解决方案是使用基数为 2 的BigInteger

BigInteger multiply = new BigInteger("01100010...01100010", 2)
   .multiply(new BigInteger("01100011", 2));

System.out.println(multiply.toString(2));

相关问题:

【讨论】:

  • 你为什么要做toString(2)而不是将2移动到第二个大整数的第二个参数?
  • 我错过了第二个参数,谢谢 ;) toString(2) 用于二进制结果打印
【解决方案3】:

以下使用带基数的 BigInteger 进行实际算术运算,并以十进制和二进制形式输出结果。

String s1 = "01100010 00110110 ...";
String s2 = "01100011";
s1 = s1.replaceAll(" ", "");
s2 = s2.replaceAll(" ", "");
BigInteger i1 = new BigInteger(s1, 2);
BigInteger i2 = new BigInteger(s2, 2);
BigInteger res = i1.multiply(i2);
System.out.println(res);
System.out.println(res.toString(2));

【讨论】:

    【解决方案4】:

    通过将基数指定为 2 来使用 BigInteger:

    BigInteger num = new BigInteger(stringVar,2);  
    

    这将另一个值乘以它:

    BigInteger result = num.multiply(new BigInteger("01100011",2);
    

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 2013-02-27
      • 2020-03-22
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多