82

Possible Duplicate:
The written versions of the logical operators.

I notice that C++ define keyword and, or, not, xor, and_eq, or_eq, not_eq and xor_eq as an alternative to &&, ||, !, ^, &=, |=, != and |=. and they're rarely used! What's wrong? Are they not portable?

Community
  • 1
  • 1
uray
  • 11,254
  • 13
  • 54
  • 74
  • 2
    See http://stackoverflow.com/questions/2376448/the-written-versions-of-the-logical-operators – James McNellis Mar 06 '10 at 18:43
  • 8
    Upvote, because even though it is the same question, your title was clearer to me. (I stumbled upon the `not`-keyword without knowing it existed.) – Unapiedra Sep 29 '11 at 10:09
  • 4
    At least Visual Studio 2015 CTP 6 did not like `or` or `not`. – usr1234567 Mar 15 '15 at 22:26
  • 8
    One problem for me is that I can never remember if `and` means `&` or `&&`. So what is the answer to `5 and 6`? 4? Wrong, it's 1. Even if `x = 5; x and_eq 6` is 4. So I prefer `&` and `&&`. More consistent. – Mr Lister Dec 30 '15 at 15:39
  • 10
    @Mr Lister strongly disagree. One should use `&` and `|` when you're about to crunch bits, and one should use `and`, `or` and `not` when your intention is to write a boolean condition. Makes for much more _clean code_ – Ichthyo May 24 '16 at 20:57
  • 4
    @Ichthyo: He has a valid argument that the alternative name for `&=` should have been `bitand_eq` rather than `and_eq`. – Ben Voigt Jun 05 '16 at 23:51

1 Answers1

38

They come from C AFAIR from times when it was not known what special symbols are on the keyboard. So to have portable language they were defined so anyone can use C even if they used keyboard with no &, |, or ^ (etc.).

Nowadays when QWERTY is a standard (with AZWERTY & co. as variations) it is no longer an issue.

PS. And of course for obfuscation code competitions ;)

Mutantoe
  • 703
  • 8
  • 20
Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61
  • 1
    PPS. And to start discussions about consistency, see `and_eq` vs `bitand` ;) – Wolf Jan 10 '17 at 12:29
  • Which header or standard are they defined in??? Once upon a time, I was using `MSVC 2010` and ended up defining my own `not` and so on... – Top-Master Jun 16 '21 at 17:55
  • @Top-Master I think they are part of syntax as keywords? – Maciej Piechotka Jun 17 '21 at 06:59
  • Thanks! I found somewhere that previously `` for `C` or `` for `C++` was required to be included (not supported by `MSVC` though), and that nowadays maybe since `C++17` and later, they are alternative tokens (without any `#define`). – Top-Master Jun 17 '21 at 07:09