# Why are booleans printed as numbers?

**URL:** <https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164>\
**Category:** General Usage\
**Tags:** boolean\
**Created:** [July 17, 2024, 9:12pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164 "2024-07-17T21:12:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [July 17, 2024, 9:12pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164/1 "2024-07-17T21:12:13Z")

</div>

With Julia 1.10.4:

```julia
julia> [("a", true), ("b", false)]
2-element Vector{Tuple{String, Bool}}:
 ("a", 1)
 ("b", 0)

```

Here true / false are confusingly printed as 1 / 0.

---

<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:** [July 17, 2024, 9:18pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164/2 "2024-07-17T21:18:43Z")

</div>

> <https://github.com/JuliaLang/julia/pull/30575>
>
> As discussed in #30298, this PR changes the printing of \`Bool\` arrays to output …\`0\` and \`1\` instead of \`true\` and \`false\`. Rationale:
> 
> \* The fact that the values are \`Bool\` is already printed in the \`Array\` type.
> \* \`0\` and \`1\` is more compact, and is equally correct since \`Bool \<: Number\`. Correspondingly, it is arguably more readable for a large number of \`Bool\` values.
> \* \`true\` and \`false\` values are confusing in an algebraic context where users are expecting numeric arrays. (This will be exacerbated by #30298 if people start using \`I(n)\` to construct identity matrices.)
> 
> Technically, a \`Bool\` value is printed as \`0\` or \`1\` by this PR whenever the container has set \`:typeinfo=Bool\` in the \`IOContext\`, indicating that the \`Bool\` type has already been printed. So, for example, \`true\` and \`false\` are still printed for \`Any\[true, false\]\`.
> 
> Example:
> \`\`\`jl
> \###### before this PR ######
> julia\> Matrix(I, 3,4)
> 3×4 Array{Bool,2}:
> true false false false
> false true false false
> false false true false
> 
> julia\> Diagonal(I, 3)
> 3×3 Diagonal{Bool,Array{Bool,1}}:
> true ⋅ ⋅  
> ⋅ true ⋅  
> ⋅ ⋅ true
> 
> \###### after this PR ######
> julia\> Matrix(I, 3,4)
> 3×4 Array{Bool,2}:
> 1 0 0 0
> 0 1 0 0
> 0 0 1 0
> 
> julia\> Diagonal(I, 3)
> 3×3 Diagonal{Bool,Array{Bool,1}}:
> 1 ⋅ ⋅
> ⋅ 1 ⋅
> ⋅ ⋅ 1
> \`\`\`

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [July 17, 2024, 9:58pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164/3 "2024-07-17T21:58:11Z")

</div>

The behavior seems to depend on exactly what you print:

```julia
julia> [["a", true], ["b", false]]
2-element Vector{Vector{Any}}:
 ["a", true]
 ["b", false]

julia> [("a", true), ("b", false)]
2-element Vector{Tuple{String, Bool}}:
 ("a", 1)
 ("b", 0)

```

Arrays of tuples trigger 0/1 printing, but arrays of `Vector{Any}` don’t trigger this. I guess the `bool` type needs to appear explicitly to trigger this behavior.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 17, 2024, 10:19pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164/4 "2024-07-17T22:19:53Z")

</div>

> [@greatpet](#):
>
> I guess the `bool` type needs to appear explicitly to trigger this behavior.

Yes, it looks at the `:typeinfo` parameter of the [`IOContext`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext) — if the type `Bool` has already been displayed (e.g. as part of the array type), then it prints the compact numerical value.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 17, 2024, 10:21pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164/5 "2024-07-17T22:21:01Z")

</div>

> [@greatpet](#):
>
> Here true / false are confusingly printed as 1 / 0.

In Julia, `Bool` values _are_ a type of number, and `true` and `false` are equal to `1` and `0`, respectively:

```julia
julia> Bool <: Integer
true

julia> true == 1
true

julia> false == 0
true

```
