Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truth table for XOR function for non binary bases

Tags:

radix

xor

ternary

I know that the XOR operator in base 2 for two bits A and B is (A+B)%2. In others words, it's addition modulo 2.

If I want to compute the truth table for XOR operation in a ternary system (base 3), is it same as addition modulo 3? Eg: In a base 3 system, does 2 XOR 2 = 1 (since (2+2)%3 = 1)?

I read this link which indicated that 2 XOR 2 in a base 3 system is 2 and I can't understand the formula behind that?

In general, for any base 'x', is the XOR operation for that base - addition modulo x?

like image 940
Bugaboo Avatar asked Mar 09 '23 17:03

Bugaboo


1 Answers

While I don't know that XOR has technically been defined in higher bases, the properties of XOR can be maintained in higher bases such that:

a ⊕ b ⊕ b = a

a ⊕ b ⊕ a = b

As the blog post shows, using (base - (a + b) % base) % base works. The part you were missing is the first instance of base. In the example of 2 ⊕ 2 in base 3, we get (3 - (2 + 2) % 3) % 3) which does give 2. This formula only works with single digit numbers. If you want to extend to multiple digits, you would use the same formula with each pair of digits just as standard XOR in binary does it bitwise.

For example, 185 ⊕ 42 in base 10 when run for each pair of digits (i.e. hundreds, tens, ones) gives us:

(10 - (1 + 0) % 10) % 10 => 9

(10 - (8 + 4) % 10) % 10 => 8

(10 - (5 + 2) % 10) % 10 => 3

Or 983 when put together. If you run 983 ⊕ 145, you'll find it comes out to 85.

like image 200
Sean Avatar answered Mar 17 '23 04:03

Sean