No, there is no
nor operator in any high level mainstream programming language.
Mainly because it is difficult to read:
- it requires the mental combination of several operators ( "and not", or in a more literary style : "further negative", "each untrue")
- it implies an implicit
not on the first operand, but the reader only understand this afterwards
- it is different from human languages, which use an explicit negation on the first operand, such as "neither x nor y", "nor x nor y". So a reader might confuse
(x nor y) with
(x and not y) instead of
((not x) and (not y))
- some readers are confused with the apparent
or semantic which doesn't apply
But it's so common in hardware...
nor is an elementary hardware gate that can be used to make all the other logical gates. So one could argue that all the other logical operators are combinations and
nor is the simplest elementary logical operator.
However, what's true for the hardware is not necessarily true to the humans. And despite it's popularity at hardware level, some mainstream CPUs do not even offer a
NOR in their assembler instruction set (e.g. x86).
Readability matters. And sometimes it can be improved by other means.
Use of existing operators
if x not in [1,2] // use of 'in' or 'not in' operator instead of x!=1 and x!=2
Ordering of conditions
if x==1 or x==2
if x!=1 and x!=2
Use of until loop
Some languages also offer loop instructions that allow to express conditions either with
while or with
until, letting you choose the more "positive" way. These instructions are for example
until c do ... in ruby,
do until c ... in vb, or
repeat ... until c in pascal and its descendants.
Until (x==1 or x==2) do
is equivalent to:
While (x!=1 and x!=2)
Make a function
Now if you still prefer the
nor syntax, you could define a function, but only if you don't expect a shortcut to happen:
If ( nor(x,y) ) // attention, x and y will always be evaluated
There is a readability advantage of the function over the operator, because the reader immediately understands that the negation applies to all arguments. In some languages you can define a function with a variable number of arguments.