# Why don't bools have (some) bitwise operators?

9 messages
Open this post in threaded view
|

## Why don't bools have (some) bitwise operators?

 Uh bools currently don't have xor so instead you have to write silly things like local a = ... local b = ... if (a and not b) or (b and not a) then    ... end And it'd look way cleaner with xor if ... ~ ... then    ... end Thoughts?
Open this post in threaded view
|

## Re: Why don't bools have (some) bitwise operators?

 On Tue, Feb 23, 2021 at 9:03 AM Soni "They/Them" L. <[hidden email]> wrote:Uh bools currently don't have xor so instead you have to write silly things like local a = ... local b = ... if (a and not b) or (b and not a) then    ... end And it'd look way cleaner with xor if ... ~ ... then    ... end Thoughts?If both values are booleans (false or true) then you can do this:local a = ...local b = ...if a ~= b then...endOtherwise doif not a ~= not b then    ...end-- Gé
Open this post in threaded view
|

## Re: Why don't bools have (some) bitwise operators?

 In reply to this post by Soni "They/Them" L. In message <[hidden email]> "Soni \"They/Them\" L." <[hidden email]> wrote:> Uh bools currently don't have xor so instead you have to write silly >things like > >local a = ... >local b = ... >if (a and not b) or (b and not a) then >   ... >end > >And it'd look way cleaner with xor > >if ... ~ ... then >   ... >end > >Thoughts? a ~= b ? --Gavin Wraith ([hidden email]) Home page: http://www.wra1th.plus.com/
Open this post in threaded view
|

## Re: Why don't bools have (some) bitwise operators?

 Look at the following:X              0  0  1  1Y              0  1  0  1X xor Y    0  1  1  0X ~= Y     0  1  1  0So in all cases, X xor Y is equivalent to X ~= Y given that X, Y are boolean. Hope that helps.~smlckz
Open this post in threaded view
|

## Re: Why don't bools have (some) bitwise operators?

 It was thus said that the Great Sudipto Mallick once stated: > Look at the following: > > X          0  0  1  1 > Y          0  1  0  1 > X xor Y    0  1  1  0 > X ~= Y     0  1  1  0 > > So in all cases, X xor Y is equivalent to X ~= Y given that X, Y are > boolean. Hope that helps.   Also, "and" and "or" in Lua short-circut evaluation.         if foo() and bar() then ... end if foo() returns false, bar() is not called (because the rest of the expression can never be true)         if foo() or bar() then ... end if foo() returns true, bar() is never called (because the expression as a whole is true)   -spc
Open this post in threaded view
|

## Re: Why don't bools have (some) bitwise operators?

 On Wed, Feb 24, 2021, 5:19 AM Sean Conner <[hidden email]> wrote:  Also, "and" and "or" in Lua short-circut evaluation.        if foo() and bar() then ... endif foo() returns false, bar() is not called (because the rest of theexpression can never be true)        if foo() or bar() then ... endif foo() returns true, bar() is never called (because the expression as awhole is true)  -spcYes, that's understandable. Short-circuiting for `and` and `or` works because in Boolean algebra, we have:        1 or A = 1        0 and A = 0for any Boolean value of A.But, in case of `xor`, you need to evaluate both sides to get the result. For example, look at the way OP was doing `xor` from its textbook definition:        (A and not B) or (not A and B)Short circuit or not: You need to evaluate both of A and B. If A=1 then the `not B` is evaluated. If A=0 then `(not A and B)` and subsequently `B` is evaluated.~smlckz
Open this post in threaded view
|

## Re: Why don't bools have (some) bitwise operators?

 On Tue, Feb 23, 2021, at 19:07, Sudipto Mallick wrote:On Wed, Feb 24, 2021, 5:19 AM Sean Conner <[hidden email]> wrote:  Also, "and" and "or" in Lua short-circut evaluation.        if foo() and bar() then ... endif foo() returns false, bar() is not called (because the rest of theexpression can never be true)        if foo() or bar() then ... endif foo() returns true, bar() is never called (because the expression as awhole is true)  -spcYes, that's understandable. Short-circuiting for `and` and `or` works because in Boolean algebra, we have:        1 or A = 1        0 and A = 0for any Boolean value of A.But, in case of `xor`, you need to evaluate both sides to get the result. For example, look at the way OP was doing `xor` from its textbook definition:        (A and not B) or (not A and B)Short circuit or not: You need to evaluate both of A and B. If A=1 then the `not B` is evaluated. If A=0 then `(not A and B)` and subsequently `B` is evaluated.~smlckz A might even be evaluated twice.C