I have two binary integers, x0
and x1
that are 8 bits (so they span from 0 to 255). This statement is always true about these numbers: x0 & x1 == 0
. Here is an example:
bx0 = 100 # represented as 01100100 in binary
bx1 = 129 # represented as 10000001 in binary
So I need to do the following operation on these numbers. First, interpret these binary representations as ternary (base-3) numbers, as follows:
tx0 = ternary(bx0) # becomes 981 represented as 01100100 in ternary
tx1 = ternary(bx1) # becomes 2188 represented as 10000001 in ternary
Then, swap all of the 1
in the ternary representation of tx1
to 2
:
tx1_swap = swap(tx1) # becomes 4376, represented as 20000002 in ternary
Then use a ternary version of OR
on them to get the final combined number:
result = ternary_or(tx0, tx1_swap) # becomes 5357, represented as 21100102 in ternary
I don't need the ternary representation saved at any point, I only need the result, where for example result=5357
. Of course I could code this by converting the numbers to binary, converting to ternary, etc.. but I need this operation to be fast because I am doing this many times in my code. What would a fast way to implement this in python?
The fastest way to do this is probably with decimal addition:
a = 1100100
b = 10000001
result = int(str(a+2*b),3) #5357
You won't find ternary-wise operators in python (or any other language that I know of.) Since you need to go above bitwise operations, your next-fastest option is integer addition, which every computer on Earth is optimized to complete.
Other solutions that convert to ternary to get this done will require you to cast back and forth to strings which takes much longer than decimal addition. This only requires one string cast at the end, assuming you even need the decimal version of the final ternary number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With