# "and", "or", bitwise or shortcircuit, what we get searching docs

**URL:** <https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263>\
**Category:** New to Julia\
**Created:** [September 8, 2020, 1:27pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263 "2020-09-08T13:27:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 1:27pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/1 "2020-09-08T13:27:37Z")

</div>

I am teaching some programming using Julia, and I faced a common student mistake which I think is justified from what we get when searching the docs. If we search “and or Julia” on google, we get, first, this page:

“Mathematical Operations and Elementary Functions…”

which defines bitwise “and” and “or”. These bitwise functions do not behave as a normal user would expect from an `and`:

```julia
julia> 1 == 1 & 2 == 2
false

```

(because `1 & 2` takes precedence).

The second page, which is

“Control flow”

defines “Short circuit evaluation”, and then the “&&” and “||” operators, which actually behave as we would expect from “and” and “or”. But that page does not mention that those are the “and” and “or” we would generally want:

```julia
julia> 1 == 1 && 2 == 2
true

```

Thus, I think that the control-flow section would benefit from a very clear explanation that `&&` and `||` are the operators that behave as `and` and `or` operators in other languages (i. e. Python).

(if I had a single saying on the clarity of the Julia syntax, it would be to define `and` and `or` as synonyms of `&&` and `||`).

Edit: Also there are no “help” entries for “and” and “or”:

```julia
help?> and
search: tand rand randn expanduser atand asind significand append! LinearIndices transcode lastindex eachindex

Couldn't find and
Perhaps you meant rand, tand, ans, any, end, randn, asind, atand, any!, Any, tan, tanh, abs, all, Cmd, Inf or bind
  No documentation found.

  Binding and does not exist.

help?> or
search: OrdinalRange for xor sort Core sort! foreach sortperm normpath sortperm! sortslices error export import

Couldn't find or
Perhaps you meant xor, for, Core, sort, one, cos, cot, eof, log, mod, IO, do, X, x, y, !, !=, %, &, ', *, +, - or /
  No documentation found.

  Binding or does not exist.

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 8, 2020, 1:43pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/2 "2020-09-08T13:43:46Z")

</div>

You can make a PR to the docs, to change most of what you mention.

> [@lmiq](#):
>
> ```julia
> julia> 1 == 1 & 2 == 2
> false
> 
> (because `1 & 2` takes precedence).
> 
> ```

Yes, regrettable. Something similar to:

```julia
julia> (1 == 1) & (2 == 2)
true

```

can sometimes be useful as an optimization (while can also be slower, so shouldn’t be the default. I guess the precedence came from C, and maybe it could be changed so the former works, in Julia 2.0 (not earlier, as a breaking change). Short circuit evaluation control-flow “operators”, should be the first you should find in the docs, and be your default, and I agree, would be nice for the help system in the REPL (I’m just not sure how easy to add, given how it’s implemented).

I note:

help?\> &&

works, and

```julia
help?> &
search: & &&

  x & y

  Bitwise and. Implements three-valued logic [..]

```

but neither point to the other, and they probably should.

About adding `and`, `or`, it’s been discussed (is a breaking change too), both as synonym, and with slightly different semantics (my proposal).

I’m just curious, who are you teaching Julia?

Googling as you did I actually got: [Control Flow · The Julia Language](https://docs.julialang.org/en/v1/manual/control-flow/) then number two the page you got. And yes, searching for “and” or “or” in the Docs search isn’t helpful…

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 1:45pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/3 "2020-09-08T13:45:39Z")

</div>

> [@Palli](#):
>
> About adding `and` , `or` , it’s been discussed (is a breaking change too), both as synonym, and with slightly different semantics (my proposal).

Why would that be breaking?

> [@Palli](#):
>
> I’m just curious, who are you teaching Julia?

Chemistry students, at the University of Campinas (Brazil)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 8, 2020, 1:51pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/4 "2020-09-08T13:51:14Z")

</div>

> [@lmiq](#):
>
> Why would that be breaking?

It would be breaking because current code can already have used it as variable names (they are not reserved names) and such code would break if they started to be parsed as operators.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 8, 2020, 1:52pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/5 "2020-09-08T13:52:39Z")

</div>

> [@lmiq](#):
>
> as a normal user would expect

I am not sure who qualifies as a “normal” user, and how you happen to know their expectations. But since you mention Python, note that

```python
>>> 1 == 1 & 2 == 2
False

```

Perhaps they are abnormal too?

> [@lmiq](#):
>
> the “&&” and “||” operators, which actually behave as we would expect from “and” and “or”

Sure, if you somehow expect them to be called “and” and “or”. But in Julia they aren’t. Since they don’t exist, they have no help entries.

Generally, the manual should not be a transition guide from Python (or C, Matlab, R, …). We have a [separate chapter in the manual](https://docs.julialang.org/en/v1/manual/noteworthy-differences/) for that purpose.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 8, 2020, 1:59pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/6 "2020-09-08T13:59:29Z")

</div>

When I think about it, it’s ~~maybe technically NOT breaking~~ (if you’re clever with parsing, EDIT: apparently that’s still a problem), I was just going by my proposal labeled like such, but as already stated you can e.g. do:

```julia
julia> or = 1 # yes, a horrible variable name, you would want to disallow this, but you COULD not do that.
1

julia> var1 or var2 # already a syntax error, so not breaking at least this
ERROR: syntax: extra token "or" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

If you allow the former, then having either (or both!) var1 or var2 be the “or” variable would be very confusing:

```julia
julia> if or or var2

```

[https://github.com/JuliaLang/julia/issues/19933](https://github.com/JuliaLang/julia/issues/19933)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 2:00pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/7 "2020-09-08T14:00:25Z")

</div>

> [@Tamas\_Papp](#):
>
> Perhaps they are abnormal too?

I meant comparing to this:

```julia
>>> 1 == 1 and 2 == 2
True

```

> [@Tamas\_Papp](#):
>
> Generally, the manual should not be a transition guide from Python (or C, Matlab, R, …).

I think in this case is common language and logic, not another programming language.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 8, 2020, 2:03pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/8 "2020-09-08T14:03:28Z")

</div>

> [@lmiq](#):
>
> Thus, I think that the control-flow section would benefit from a very clear explanation that `&&` and `||` are the operators that behave as `and` and `or` operators in other languages (i. e. Python).

While I agree that `and` and `or` are more readable, `&&` and `||` are used in `C`, `C++`, `Java` and many other `C-like` languages. So it probably is missing from the docs because whoever wrote it thought this was common knowledge. [I made a PR to improve it.](https://github.com/JuliaLang/julia/pull/37463)

> [@lmiq](#):
>
> we get, first, this page:
> 
> “Mathematical Operations and Elementary Functions…”

Funny, to me google returns the `Control Flow` page first and `Mathematical Operations and Elementary Functions` second.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 2:05pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/9 "2020-09-08T14:05:36Z")

</div>

Anyway, I am not saying that it is very important to have `and` and `or` as `&&` and `||`, although that would be cute.

What I am saying is that the docs and the help pages lead to confusion, and there is no mention anywhere that `&&` and `||` is what we should most commonly use for conditionals where we logically would use `and` and `or`.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 8, 2020, 2:08pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/10 "2020-09-08T14:08:07Z")

</div>

> [@Palli](#):
>
> When I think about it, it’s maybe technically NOT breaking (if you’re clever with parsing),

You are forgetting about whitespace sensitive contexts like in array expressions or macros. So for example `[a or b]` would parse as a single element vector instead of 3 hcatted ones like it does currently. That’s why I’m sceptical about adding further word operators, since they make it more difficult to tell what’s actually going on.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 8, 2020, 2:09pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/11 "2020-09-08T14:09:11Z")

</div>

> [@lmiq](#):
>
> I meant comparing to this:
> 
> ```julia
> >>> 1 == 1 and 2 == 2
> True
> 
> ```

But note that Python’s `and` is `&&` in Julia, so you already have that.

> [@lmiq](#):
>
> common language and logic

There is nothing “logical” in calling that operator `and` as opposed to `&&`. Various languages have different conventions, you cannot derive this from first principles. At least not spelling them out is consistent (cf `1 + 1` vs `1 plus 1`).

> [@lmiq](#):
>
> we logically would use `and` and `or`

We would, if we were using another programming language that uses these operators. Again, you appeal to “logic”, but these are (arbitrary) syntactic decisions.

In any case, this is unlikely to change now (unless, of course, we [all go Zen](https://discourse.julialang.org/t/feedback-make-julia-as-pythonic-as-possible/46216/11)).

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 8, 2020, 2:09pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/12 "2020-09-08T14:09:12Z")

</div>

> [@lmiq](#):
>
> Chemistry students, at the University of Campinas (Brazil)

Off-topic: Have you taught other languages (which?) [to beginners]? Did it go better or worse? Why did you choose Julia, is it about the language, or mostly about some package[s]?

I’m thinking Julia could be good for beginners, after you get over some small issues like this, shared by many other languages. I assume chemistry students are in many cases (not all) new to programming (if not, this likely wouldn’t be an issue for you).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 2:20pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/13 "2020-09-08T14:20:55Z")

</div>

I insist, I am not suggesting that we should add those keywords, if that is complicated. I just think that would have been nice.

But I am not comparing programming languages here. I am just saying that if a new user searches the docs it might start using `&` where `&&` is the correct thing to use. And that is because in common language “and” is “and”, and what we get from the docs is:

`In the expression `a && b`, the subexpression`b`is only evaluated if`a`evaluates to`true` .`

or

`x & y	bitwise and`

the english word `and` is in the second definition, not in the first. Related to this:

> [@Palli](#):
>
> I’m thinking Julia could be good for beginners, after you get over some small issues like this, shared by many other languages. I assume chemistry students are in many cases (not all) new to programmers (if not this likely wouldn’t be an issue for you).

This in particular is not a big problem, if we warn the students soon enough. Other small things (which other languages share), are slightly more confusing, for example the difference between single name assignment and copying.

Yes, these students are VERY beginners, they commonly never had any contact with any other programming language. I have used Fortran and Python before. I want to teach them how to solve the dynamics of chemical kinetics and some particle simulations, and I do not use almost any package (except Plots). I usually teach them how to implement the loops explicitly for solving coupled differential equations, and only at the end of the course I show them how to feed packages with efficient implementations of the same concepts.

It has been a nice experience using Julia for that.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 2:26pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/14 "2020-09-08T14:26:20Z")

</div>

> [@Palli](#):
>
> I’m thinking Julia could be good for beginners, after you get over some small issues

One issue has been package installation. I have to teach them in a student computer laboratory, and the students have limited storage space. Installing Julia and a few packages takes GBs of disk, and that is not possible. Installing Julia in a shared environment has been an issue.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 8, 2020, 2:35pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/15 "2020-09-08T14:35:16Z")

</div>

> [@lmiq](#):
>
> I am just saying that if a new user searches the docs it might start using `&` where `&&` is the correct thing to use. And that is because in common language “and” is “and”, and what we get from the docs is

I am not sure why you think that:

1. _searching_ the docs with `?and` will turn up nothing, as that is not a keyword or operator in Julia,

2. the docstrings of `&` and `|` point out that these are _bitwise_ operators **in the first sentence** , and even have examples to this effect.

> [@lmiq](#):
>
> in common language “and” is “and”

Sure, that’s why the docs use the words “bitwise” and “short-circuiting boolean” **before** each “and” in the docstrings.

I am genuinely puzzled how confusion could arise.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 8, 2020, 2:39pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/16 "2020-09-08T14:39:16Z")

</div>

Well, to be truth, I still do not understand what `bitwise` is and where ever we would use that. And I have programmed for 20 years now, and developed some popular packages in my domain. I am teaching computer programming to people who don’t know in advance what an ASCII file is and start coding in Word. “Short-circuit boolean” also does not mean anything to most people, particularly for non-fluent-english speakers.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [September 8, 2020, 2:39pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/17 "2020-09-08T14:39:39Z")

</div>

I would also add that searching for “and” in the docs doesn’t give any link to &, &&, control flow, or bitwise operators at all. But there were many threads criticising julia docs search already 🙂

Personally I always have to resort to google search in julia docs. The content is great and very useful, the search - not so much.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 8, 2020, 2:45pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/18 "2020-09-08T14:45:21Z")

</div>

> [@lmiq](#):
>
> takes GBs of disk, and that is not possible. Installing Julia in a shared environment has been an issue.

That’s possible! Given enough disk space… 🙂 Anyway, you could look into Juliabox (at was a free solution, then paid, still is?), I see the website(s) are gone, but he software still exists, and could be installed at your institution. It’s at least a hosted environment, I guess the storage for packages was (to some degree only?) shared.

For a Julia cluster/HPC, I doubt each node (pre)compiles and stores its own packages. I assume the precompilation needs to only be done once, so maybe some shared disk for all students it possible (even allowing non-shared packages?), just to save space. [I believe HPC nodes do some redundant work when they load precompiled packages, as there compilation step is not fully finished, I however think that part is only in memory.]

> <https://github.com/JuliaLang/www.julialang.org/commit/11778b41e2f41359e83fbe52768281c940e1ea90#diff-5e113ce6bf37296640a6ae47c9bd4c94>

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 8, 2020, 2:56pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/19 "2020-09-08T14:56:57Z")

</div>

> [@lmiq](#):
>
> I still do not understand what `bitwise` is and where ever we would use that

> **[Bitwise operation](https://en.wikipedia.org/wiki/Bitwise_operation)**
>
> In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.
> On simple low-cost processors, typically, bitwise operations are substantially faster than...

> [@lmiq](#):
>
> “Short-circuit boolean” also does not mean anything to most people, particularly for non-fluent-english speakers.

It is documented in detail, with examples:

[https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation)

Generally, programming (like all fields or specialties) is full of terminology that is not self-explanatory, regardless of one’s native language. Expecting _everything_ to be self-explanatory is a somewhat unusual approach to learning.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 8, 2020, 2:57pm UTC](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263/20 "2020-09-08T14:57:53Z")

</div>

Yes, “Short-circuit” is a CompSci term (or actually taken from electrical engineering), just something you need to know happens, it’s simple. But it means kind of that e.g. “&&” is not “and” as in English in general (and even if people think it might be better, wouldn’t be to people in e.g. Brazil). There are other issues with “or” in English meaning “||” or xor (either, or), so best to get used to that, and I kind of support not having “and” and “or” for it.

Bitwise however means you do for each corresponding bit; and in the context of & and | if there’s only one bit (true or false; as in a condition in an if statement) you get the meaning of “and” and “or” what you would really want it to mean (w/o short-circuiting).

Bitwise & and | (on many bits), is useful, I guess mostly outside your domain (e.g. in crypto, and for graphics when it was black-and-white)? E.g. if you have many flags in one integer. It’s a rather low-level concept, was/is used to conserve memory for flags. I wouldn’t worry to much about it.

It’s a bit dangerous in C and C++ for control flow, but shouldn’t be in Julia, except for the precedense. It’s rarely used as very low-level optimization.

[Next page](https://discourse.julialang.org/t/and-or-bitwise-or-shortcircuit-what-we-get-searching-docs/46263.md?page=2)
