Given this logical operation :
(A AND B) OR (C AND D)
Is there a way to write a开发者_JAVA百科 similar expression without using any parentheses and giving the same result ? Usage of logical operators AND, OR, NOT are allowed.
Yes:
A and B or C and D
In most programming languages, and
is taken to have higher precedence than or
(this stems from the equivalence of and
and or
to *
and +
, respectively).
Of course, if your original expression had been:
(A or B) and (C or D)
you couldn't simply remove the parentheses. In this instance, you'd have to "multiply out" the factors:
A and C or B and C or A and D or B and D
How about A AND B OR C AND D
? It's the same because AND
takes precedence over OR
.
Just don't put any parentheses, it is the same...
It can be written in two ways
- A & B | C & D
- Type as it is mentioned in question just remove the parenthesis it will show the same result.
We can use &
for AND
to multiply and |
for OR
to divide. Also simply you can write them without any parenthesis
精彩评论