Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

Alternative operator tokens: and, or, not

Reference for C++ alternative operator tokens such as and, or, not, bitand, xor, plus digraphs and trigraph history, with the full mapping table.

C++ defines word spellings for several operators. and means &&, or means ||, not means !, and so on. They are not macros, as the similar names in C's <iso646.h> are: they are real tokens built into the language, so they are always available and cannot be redefined. They exist for keyboards and character sets that lack characters such as &, |, ^, ~ or !.

Most code uses the symbols, but some teams prefer and/or/not for readability in conditions. Both spellings are 100% equivalent and can be mixed, although mixing in one expression looks odd.

C++
if (not done and (a or b)) { /* same as !done && (a || b) */ }

Try it

C++ Words instead of symbols

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Words are tokens, not macros

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

Alternative Symbol Meaning
and && logical AND
or || logical OR
not ! logical NOT
not_eq != inequality
bitand & bitwise AND (and address-of)
bitor | bitwise OR
xor ^ bitwise XOR
compl ~ bitwise complement
and_eq &= AND assignment
or_eq |= OR assignment
xor_eq ^= XOR assignment

Digraphs: <: is [, :> is ], <% is {, %> is }, %: is #, and %:%: is ##. (Trigraphs such as ??= were removed in C++17.)

Errors: none specific. Using one of these words as a variable, function or macro name is ill-formed.

Usage notes

  • Same precedence: an alternative token has exactly the precedence and associativity of its symbol; a and b or c is (a && b) || c. See precedence.
  • Bitwise versus logical: xor is the bitwise ^ and has no logical counterpart; on bool operands it gives the logical exclusive-or result, but there is no short-circuiting. bitand and and are different operators.
  • Overloading: write operator&& or operator and; both name the same function. Using the symbol form in declarations is the usual convention.
  • Unary bitand is the address-of operator: bitand x is &x. This is legal but confusing; don't write it.
  • Reserved names: because they are tokens, and, or, not, xor, compl, bitand, bitor, and_eq, or_eq, xor_eq and not_eq can't name your own variables. This breaks code ported from C that used them as identifiers, and C programmers need <iso646.h> to get the same spellings.
  • Microsoft's compiler needs /permissive- (default in newer language modes) or <iso646.h> for these words; GCC and Clang always accept them.
  • Style: most style guides use the symbols; choose one spelling per code base. The words read well for not and and in conditions but make bitand/xor hard to scan.
  • Digraphs exist only for restricted character sets; one place you may meet them is <:: handling in templates (std::vector<::std::string> is fine since C++11, because <: is not treated as a digraph before ::).