How can I implement bitwise operators in Lua language?
Specifically, I need a XOR operator/method.
相关问题
- How to get the return code of a shell script in lu
- Require explanation for the output
- Accessing Light userdata in Lua
- Logical vs. bitwise operator AND
- Cast some light on population count algorithm
相关文章
- Construction an logical expression which will coun
- Rounded division by power of 2
- Lua Integer type
- Check for multiple values when using comparison op
- First character uppercase Lua
- torch / lua: retrieving n-best subset from Tensor
- How to handle errors when resuming a Lua thread (c
- Is Lua an object-oriented language?
This is very simple. use NAND logic. https://en.wikipedia.org/wiki/NAND_logic
if you also need 1,0 inputs insert the following to the function
Hope this helps someone.
Since you're referencing the floor function 3 times, using an excessive number of loops for most operations (numbers less than 2^31 don't need all 31 loops), are using the ^ operator, and aren't capitalizing on the fact that a and b might be wildly different numbers with different magnitudes, you're losing a lot of efficiency. The function also isn't localized, and you're doing two more division operations than you need to. I wrote this to be reasonably fast.
In general, you're going to see improvements of about 3 to 20 times.
If you need more than this, say AND, OR, and NOT, then I've got you covered there, too.
Don't worry, you won't need to change anything.
In Lua 5.2, you can use the
bit32.bxor
function.From the OP; moved from question into this answer.
This is how I implemented XOR in Lua:
In Lua 5.2, you can use functions in
bit32
library.In Lua 5.3,
bit32
library is obsoleted because there are now native bitwise operators.Output:
If you're needing an efficient way to do bitwise shifts, I wrote an article about that a while ago. Here's some functions which wrap the technique: