Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
And she is correct
>>> def foo(x, y):
>>> print((x and (not y)) or ((not x) and y))
>>>
>>> def bar(x, y):
>>> print(x != y)
>>>
>>> def printer(func):
>>> func(False,False)
>>> func(False,True)
>>> func(True,False)
>>> func(True,True)
>>> printer(foo)
>>> print("--------")
>>> printer(bar)
False
True
True
False
--------
False
True
True
False -
Yes, thanks @Voxera.
I forgot to mention javascript tag, but I was able to find a simplified ternary operator version for this through her comment.
(x ? !y : y) -
hitko31483y@shehanthamel (!x ^ !y), or (!x != !y) (if x and y are already both booleans, you can just write x^y or x!=y)
-
@shehanthamel That works, but I'd probably use the first just for the sake of writing clear code.
If they're bools, perhaps x !== y? -
arekxv10543y@shehanthamel
If its Javascript then you can do one of:
x ^ y - runs xor for x and y, works well in this case if x and y are bool only. Returns 1 or 0.
!!x ^ !!y - converts to bool and runs xor. Returns 1 or 0.
!!(!!x ^ !!y) - converts to bool, runs xor, converts result to bool true / false.
So pick one for your use case :)
Related Rants
Is there a way to simplify the following boolean expression..??
(x && !y) || (!x && y)
question
expression
boolean
simplify