34
4
You goal is to implement the operation of XOR (carryless) multiplication, defined below, in as few bytes as possible.
If we think of bitwise XOR (^
) as binary addition without carrying
101 5
^ 1001 9
----
1100 12
5^9=12
we can perform XOR multiplication @
by doing binary long-multiplication but doing the adding step without carrying as bitwise XOR ^
.
1110 14
@ 1101 13
-----
1110
0
1110
^ 1110
------
1000110 70
14@13=70
(For mathematicians, this is multiplication in the polynomial ring F_2[x]
, identifying polynomials with natural numbers by evaluating at x=2
as a polynomial over Z.)
XOR multiplication commutes a@b=b@a
, associates (a@b)@c=a@(b@c)
, and distributes over bitwise XOR a@(b^c)=(a@b)^(a@c)
. In fact, it is the unique such operation that matches multiplication a@b=a*b
whenever a
and b
are powers of 2
like 1,2,4,8...
.
Requirements
Take two non-negative integers as input and output or print their XOR-product. This should be as numbers or their decimal string representations, not their binary expansions. Fewest bytes wins.
Don't worry about integer overflows.
Here are some test cases formatted as a b a@b
.
0 1 0
1 2 2
9 0 0
6 1 6
3 3 5
2 5 10
7 9 63
13 11 127
5 17 85
14 13 70
19 1 19
63 63 1365
13
This is better known as "carry-less multiplication", which you might want to add the the question title, and with high probability the smallest entry is the 6-byte x86 instruction
– Iwillnotexist Idonotexist – 2015-05-16T16:14:06.423PCLMULQDQ
from the CLMUL extension. Unfortunately I got downvoted for my knowledge of the x86 instruction set before (Related toPEXT/PDEP
), so I'm going to just leave this as a comment here.@IwillnotexistIdonotexist Thanks for the note, it's nice to have a name to Google. – xnor – 2015-05-16T21:04:27.957
If that above is not "xor" you have to call in a different way as xorc or xornc ... It is not xor – RosLuP – 2016-12-04T09:33:39.330
1@RosLuP It's not xor, it's xor multiplication. – xnor – 2016-12-04T09:39:57.990
@boboquack Actually, I believe nimber multiplication is different. For instance, it has 2*2==3. Both of these distribute over nim addition, but the one in this challenge matches multiplication on powers of 2, whereas the nimber on matches only on 2^(2^n).
– xnor – 2016-12-04T23:39:47.977