How do I use the bitwise operator XOR in Lua?

后端 未结 6 947
一向
一向 2020-12-30 05:38

How can I implement bitwise operators in Lua language?
Specifically, I need a XOR operator/method.

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 06:03



    This is very simple. use NAND logic. https://en.wikipedia.org/wiki/NAND_logic

    function xor(a,b)
        return not( not( a and not( a and b ) ) and not( b and not( a and b ) ) )
    end
    

    if you also need 1,0 inputs insert the following to the function

        a = a==1 or a == true   -- to accept nil, 1, 0, true or false
        b = b==1 or b == true   -- to accept nil, 1, 0, true or false
    

    Hope this helps someone.

提交回复
热议问题