3

How do I execute binary operations in Mathematica? I want to to multiply say 1010101011 (binary) to 1111101110 (binary) and getting the result 10100111101111111010? Then I want to add the result to say 10101010000111 (binary)? Assume the all binary numbers are given as lists such as {1,0,1...}. Thanks!

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Giorgio
  • 341
  • 2
  • 5
  • You could zero-pad (upsample) by 2*log2(length smaller operand) or something like that, and use ListConvolve[l1,l2,,{1,-1},Modulus->2]. – Daniel Lichtblau Mar 31 '14 at 22:26

2 Answers2

10

There isn't really such a thing as binary arithmetic (at least in Mathematica). Numbers can be represented in any base, and this user-visible representation is completely independent from how arithmetic is done.

Try this:

BaseForm[(2^^1010101011)*(2^^1111101110), 2]

Things to look up:

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
6

You can easily convert between decimal representation and a representation in terms of binary lists with IntegerDigits and FromDigits as in the following example:

IntegerDigits[57, 2]
{1, 1, 1, 0, 0, 1}
FromDigits[%, 2]
57

It is then straight forward to write functions for arithmetic like for example:

Multiply[a_, b_] := IntegerDigits[FromDigits[a] FromDigits[b], 2];

To get a multiplication operator symbol you can use for example:

MakeExpression[RowBox[{x_, "x", y_}], StandardForm] := 
MakeExpression[RowBox[{"Multiply", "[", x, ",", y, "]"}], StandardForm]

This then make to the following evaluation possible:

{1, 0, 0} x {1, 0, 1}
{1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0}

Maybe it would be better to use another symbol than "x"...

cgogolin
  • 415
  • 3
  • 8
  • I'm looking for something like {1,1,1,0,1} x {1,1,1,0,1} = {1, 0, 1, 0, 0, 1, 1, 0, 1, 1} – Giorgio Mar 28 '14 at 16:14
  • I see. I edited the answer accordingly. You might also find http://mathematica.stackexchange.com/questions/31375/how-is-as-an-infix-operator-associated-with-plus/31377#31377 interesting. – cgogolin Mar 28 '14 at 16:27
  • Hi @Cgogolin, thanks! What I'm trying to do is to find the fastest way to multiply a binary number say b1 (with is list with one million components of zeros and ones) times b2 also a list with one million components of zeros and ones. Are you familiar with the Karatsuba Multiplication method? It claims to be much faster than conventional multiplication. The result should be a list with 2 million bits. I'll try your suggestion above and will see what happens, specially when I feed it with 2 lists of one million components each. – Giorgio Mar 29 '14 at 00:51
  • I wasn't aware that the numbers were that large. I was trying to come up with a convenient method, rather than an efficient one. No, I am not familiar with Karatsuba Multiplication. – cgogolin Mar 31 '14 at 08:10
  • @Giorgio If the purpose is to obtain "faster" integer multiplication, this approach is not going to get you there. – Daniel Lichtblau Mar 31 '14 at 22:07
  • Hi guys, thanks for all the suggestions. What I'm trying to do is to simulate a virtual cpu which only manipulates zeros and ones and have a small set of instructions namely addition, multiplication, subtraction and division. This virtual cpu also have 4 registers where to put temp operations. I tried to see how fast this machine I work on is, by multiplying two numbers of one billion decimal digits each (8 billion bits each).I started with IntegerLength[x = 2^3321928094] to generate a one billion digits number x and multiplied it by itself. The result Timing[x*x;] was {0.125000, Null}. – Giorgio Apr 07 '14 at 15:46
  • This means the machine I'm working on is reasonably fast and multiplied two 8 billion bits in 1/8 of a second. In the programs suggested above I tried to multiply 2 binary numbers of one million bits each and the time was 0.234 seconds. So, transforming the list into decimal, multiplying them in decimal and converting them back to binary is slower than multiplying 2 one billion digits each. So, I will try to program the binary arithmetic and see what happens. – Giorgio Apr 07 '14 at 15:46
  • @Giorgio, Karatsuba is already done internally by Times[] and numbers are internally stored as binary anyway, so your proposed route will not gain much. – J. M.'s missing motivation Aug 04 '15 at 08:27