2

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!

4
  • 2
    can you use BigInteger? Commented Dec 29, 2013 at 17:39
  • I could, but i'm not sure how i would do binary multiplication with that... as i've never used biginteger... Commented Dec 29, 2013 at 17:43
  • call multiply() numbers are numbers, regardless of what base they are displayed in Commented Dec 29, 2013 at 17:44
  • BigInteger has a constructor that takes a string and a radix, and a toString method that takes a radix argument, as well as a multiply method. Commented Dec 29, 2013 at 17:45

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

OP will have to remove the spaces.
new BigInteger("00110001 01100001 00110111 00110110 00110101 01100010".replace(" ",""),2)
2

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:

2 Comments

why are you doing toString(2) instead of moving the 2 to the second parameter of the second biginteger?
I miss second parameter, thanks ;) toString(2) is for binary result printing
2

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.