Truth Table
First, it is good to review the concepts of the truth table, to understand how to input and boolean values work.
A truth table is nothing more than a logic machine that for a given input or value and its output is calculated according to logical, functional, and Boolean predeterminations
AND and OR
Recently I was faced with a lack of knowledge regarding Booleans, I didn't know how to convert a &&(AND) to ||(OR) in the language I was using (JavaScript), I would have to do a validation converting AND to OR so I focused on these two logical operators in this post.
Logical operators are nothing less than mathematical functions and expressions, yes the ones you probably studied in school.
- The logical operator AND also called logical conjunction, is used for given two value inputs where it produces a true value if both operands are true. In programming logic, there are two values in bits that represent true or false which are: 1 and 0
- Logical OR operator also called Logical Disjunction, given two inputs with values, it produces a true value if at least one of its operands is true.
Logical Operator XOR
The logical operator XOR represents the inequality function that is if both inputs are true or false the output will be false (0) if both inputs differ it will return true. A nice phrase to remember the logic of XOR is :
"must have one or the other, but not both "The analytical presentation of this logical operator is given by the expression:
f(a,b)=a+b-2ab
- An interesting curiosity that you may not have realized yet is that the logical operator XOR is the conversion of the AND and OR operators, and so when you have a logical AND and want to turn it into a logical OR you will use the XOR.
Logical operator XOR in JavaScript
- After a summary of logical operators and truth table we can now get back to solving the problem that triggered this post.
- As in the truth table there are also logical operators in programming languages, and how would be the XOR operator in JavaScript?
Simple, since we do not have a real symbol for this operator we can represent it with the conversion of the AND to OR operators that would look like this:
bCondition1 && bCondition2
to
!(!bCondition1 || !bCondition2)
I hope this post has helped you or added something to it \o/
For feedback on talk to me on Twitter
If you want to continue supporting my content Patreon
My GitHub https://github.com/biantris
Version in Pt-BR 🇧🇷: https://dev.to/beatrizoliveira/convertendo-and-para-or-em-javascript-4ohd
Top comments (3)
This triggered a curiosity: why would you want to convert AND to OR?
Example: When you use Regex to filter somethings, Regex doesn't have AND operator
Great post!!!