13

The Vertical Bar Operator (|) is used in a variety of constants to mean OR in some way:

C-style languages use a | b to mean bitwise OR of a and b, a || b to mean logical OR of a and b.

Regular expressions use a|b to mean match with a or match with b.

Context Free Grammars use x := a | b in much the same sense as regexes.

While &for AND makes sense, why is | used for OR?

9
  • 7
    Because that's what the language designers chose. There isn't any other character on the keyboard that intrinsically means "or" more than the vertical bar does. In fact, all of the other characters have some intrinsic meaning that is not a or, so you could make the argument that they chose it because it is the most neutral. May 9, 2016 at 0:03
  • @RobertHarvey A citation would be useful, but that seems plausible. Although, did | as a pipe come first?
    – k_g
    May 9, 2016 at 0:06
  • 6
  • 7
    An awful lot of languages copy from C. May 9, 2016 at 2:20
  • Amusing: an accepted answer for a question deemed off-topic at stackoverflow would be perfect for this question which is on-topic here. Migrate that and Close this (as Duplicate)? Mar 16, 2017 at 23:45

2 Answers 2

7

A very good answer is provided on Stack Overflow.

The core of the explanation is:

In Backus-Naur form, an expression consists of sequences of symbols and/or sequences separated by '|', indicating a choice, the whole being a possible substitution for the symbol on the left.

<personal-name> ::= <name> | <initial>

Of course, the next question could be (tnx, @Rob):

"Why is a vertical bar used as the OR operator in Backus-Naur?"

But that is another question, with a merit in itself :)

My assumed answer: because they had to use something, and nothing that was available could be used non-ambiguously.

  • / is the division symbol, path separator in *nix systems

  • \ (today it is path separator in DOS / MS Windows systems)

  • +-* are standard mathematical operators

and so on...


From a comment from @amon:

Backus introduced the grammar notation in The Syntax and Semantics of the Proposed International Algebraic Language where he just used the word "or". Backus et al with Naur as editor replaced this with the symbol "|" in their Revised Report on Algol 60 (original report 1960, revised report 1962) with no explicit reason, probably for brevity. All of this predates Unix by roughly a decade, and slightly predates the ASCII standard.

8
  • 4
    Then the question becomes "Why is a vertical bar used as the OR operator in Backus-Naur?"
    – Rob
    Mar 1, 2019 at 11:03
  • @Rob: no, it does not become anything. You just formulated another question, with a merit in itself :) Assumption: because they had to use something, and nothing that was available could be used non-ambiguously.
    – virolino
    Mar 1, 2019 at 11:07
  • I agree with you but it does beg the question.
    – Rob
    Mar 1, 2019 at 11:09
  • 1
    I don't think "Why is it used in Backus-Naur" would justify a separate question. Yes, it's not technically a "programming language", so by a strict reading of the question would be out of scope; but it's still clearly in the realm of computer science, so it feels pedantic to demand a separate answer just to change that scope.
    – IMSoP
    Mar 1, 2019 at 11:44
  • 4
    Backus introduced the grammar notation in The Syntax and Semantics of the Proposed International Algebraic Language where he just used the word "or". Backus et al with Naur as editor replaced this with the symbol "|" in their Revised Report on Algol 60 (original report 1960, revised report 1962) with no explicit reason, probably for brevity. All of this predates Unix by roughly a decade, and slightly predates the ASCII standard.
    – amon
    Mar 1, 2019 at 13:25
3

The main reason for choosing certain characters has been accessibility of said characters. Today, we have Unicode and we can easily type characters such as with our Compose Key¹. That arrow was used as the assignment in APL (a ← b + c).

IBM had a specialized keyboard that allowed you to type those special APL (math, really) characters:

enter image description here

But various solutions were looked at to provide ways for regular people to type programs without the need for those special characters. For example, the APL operator actually became the := operator still in use in Pascal and Ada (and invalid use in Go & Perl).

C had a similar issue and also clearly mentioned the fact that someone with just ASCII characters should be able to enter programs. Actually, it was "the old ASCII" without the characters 0x60 to 0x7E (the ```, lowercase a to z, {, |, }, ~). So the digraph (Pascal (* and *) for comments) and the trigraph (C ??! for the | character) were introduced. Now the trigraph have been deprecated and by default are not supported in C/C++ (finally).

That being said, the reason for the | in the C language (which is the first programming language using that operator), is most certainly following the choice in Backus-Naur as mentioned by @virolino above. The math operators are the "∨" for OR and "∧" for AND, which we cannot type in ASCII and definitely do not look like the | character. Note, however, that the original symbols were different:

Naur changed two of Backus's symbols to commonly available characters. The ::= symbol was originally a :≡. The | symbol was originally the word "or" (with a bar over it).[7]: 14

In other words:

__
or  became  |

It is not clear to me which came first, Naur's change or the C language. My guess is that Naur's change happened first.


¹ The Compose Key has been available on Ubuntu for years. If you did not turn it one when installing Ubuntu, you'll find it in your preferences. Look here for MS-Windows and here for Mac OS.

3
  • That left arrow is not easy to type (on a Mac, which tends to be quite flexible). On the other hand if programming languages started using it as an assignment operator, I bet it would become easy.
    – gnasher729
    Jun 27 at 21:45
  • @gnasher729 I added a couple of links about this. The Compose Key is really useful to me. Although frankly, for programming, I still think using regular ASCII is best (except for strings in languages requiring Unicode). Now I have seen more and more people writing using their language and thus "weird" characters in their functions, types, and variable names... Jun 27 at 22:51
  • @AlexisWilke: I guess that "or" (with a bar over it) was usable only on paper. Even Fortran (created by Backus) uses .or. for "or".
    – virolino
    Jun 28 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.