Doing arithmetic with binary numbers

2

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!

Giorgio

Posted 2014-03-28T15:39:29.890

Reputation: 341

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 – 2014-03-31T22:26:59.257

Answers

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

Posted 2014-03-28T15:39:29.890

Reputation: 213 047

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

Posted 2014-03-28T15:39:29.890

Reputation: 415

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 – 2014-03-28T16:14:50.993

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 – 2014-03-28T16:27:19.673

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 – 2014-03-29T00:51:22.163

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 – 2014-03-31T08:10:50.353

@Giorgio If the purpose is to obtain "faster" integer multiplication, this approach is not going to get you there. – Daniel Lichtblau – 2014-03-31T22:07:58.507

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 – 2014-04-07T15:46:09.720

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 – 2014-04-07T15:46:27.793

@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 ennui – 2015-08-04T08:27:21.203