# Why not make + and sum() operators between Booleans act as bitwise operators?

**URL:** <https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [January 14, 2020, 4:46pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350 "2020-01-14T16:46:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emre\_Acarturk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emre_acarturk/32/9023_2.png) [@Emre\_Acarturk](https://discourse.julialang.org/u/Emre_Acarturk)\
**Post date:** [January 14, 2020, 4:46pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350/1 "2020-01-14T16:46:54Z")

</div>

I feel like this change wouldn’t affect Julia in general, but AND and OR operation between Boolean variables are usually denoted as addition and multiplication and result in a Boolean. In its current implementation, `sum(Array{Bool,1})` returns an `Int64` whereas `prod(Array{Bool,1})` returns a `Bool`, which is to say products are defined equivalent to AND but addition is not equivalent to OR.

I know modulo 2 addition is the XOR operation and thus this implementation is not useless, but I feel usually `sum(Array{Bool,1})` is used to see if any one entry is correct, where I know `sum(someArray)>0` does the job. Any opinions?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [January 14, 2020, 5:02pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350/2 "2020-01-14T17:02:07Z")

</div>

We have `any` and `all`, so what would be the point of having `sum` do the same thing?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 14, 2020, 5:04pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350/3 "2020-01-14T17:04:38Z")

</div>

Welcome, Emre! Boolean `or` could be the same as addition — but only if it’s done mod 2. We’ve simply found it helpful to widen small integers to avoid overflow as it’s an easy trap, and Julia’s integer addition is generally at least mod 32 or 64.

We also have `count`, which does the same thing as `sum` for collections of bools.

---

<div class="post-metadata">

**Author:** ![Emre\_Acarturk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emre_acarturk/32/9023_2.png) [@Emre\_Acarturk](https://discourse.julialang.org/u/Emre_Acarturk)\
**Post date:** [January 14, 2020, 5:16pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350/4 "2020-01-14T17:16:32Z")

</div>

Yep, these functions are what I was suggesting (and apparently well optimized). Though I am not entirely sure _why_ we should make `Bool`s add up to `Integer`s (well, except for the XOR remark).

> [@nalimilan](#):
>
> what would be the point of having `sum` do the same thing?

You’re right, but `prod` and `all` _are_ the same. It’s somewhat confusing to add `Bool`s to start with since, you know, Boolean arithmetic doesn’t have addition. So I suggest to equate Boolean addition to OR operation because other operations doesn’t make a lot of sense (without casting).

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 14, 2020, 6:03pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350/5 "2020-01-14T18:03:32Z")

</div>

Julia’s `Bool` is a subtype of `Integer`, so they do behave like them in some contexts. It’s not a perfect situation, but it works — and I’d argue it’s more helpful in practice than it is hurtful.

There are a number of places where we favor practicality over theoretical purity.

---

<div class="post-metadata">

**Author:** ![Emre\_Acarturk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emre_acarturk/32/9023_2.png) [@Emre\_Acarturk](https://discourse.julialang.org/u/Emre_Acarturk)\
**Post date:** [January 14, 2020, 7:34pm UTC](https://discourse.julialang.org/t/why-not-make-and-sum-operators-between-booleans-act-as-bitwise-operators/33350/6 "2020-01-14T19:34:45Z")

</div>

Makes sense, and rightfully so. Since any new behavior one can suggest already exists in the language (and semantic purity is not _the_ goal) I guess my suggestion becomes meaningless. Thanks, Matt and Milan.
