Why is the negation of a Boolean array a BitArray

If b = rand(Bool, 3)

then all of

  1. .!b
  2. .~b
  3. xor.(true, b)

return a BitArray{1} type. Given that the input types are always Array{Bool, 1}, I find that curious. Is this behavior wanted, and if so, why?

1 Like

Smaller memory size and faster vectorized bit-wise operations?
See also: https://docs.julialang.org/en/v1/base/arrays/#Base.BitArray

1 Like

That makes sense. Wouldn’t it then be more consistent to have rand(Bool, 3) return a BitArray too? I just find it surprising that these functions do not preserve the type.

1 Like

No, it would be inconsistent, as rand(T, n) gives Vector{T}s. See bitrand.