简体   繁体   中英

Long binary string arithmetic in java

I'm doing a program, server based, that I want to multiply 2 binary strings together. these strings are quite long, so they arent transferable into long or int types. an example of one of these strings is:

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

this string can have the spaces, or not. it doesnt matter. my problem is, how do i multiply, lets say, 01100011 with this string? the length of the multiplier is variable, so flexibility would be a must.

Thanks in advance!

Just use BigInteger and specify the radix:

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

you'll find that it supports arithmetic operations like multiply , add , etc. So for example, after creating the variable above, we could say:

BigInteger bigProduct = bigNumber.multiply(someOtherBigNumber);

assuming we've created another BigInteger

EDIT : As has been pointed out in the comments, you'll need to remove the spaces in your binary string representation, which is easy to do - I've updated my answer to include this step.

The simplest solution is to use BigInteger with radix 2.

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

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

Related questions:

The following uses BigInteger with a radix to do the actual arithmetic and outputs the result in both decimal and binary form.

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));

Use BigInteger by specifying the radix to 2 for binary:

BigInteger num = new BigInteger(stringVar,2);  

And this to multiply the other value to it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM