How to invert bits in a bit array

A bit silly question, but I couldn’t find anything on how to (properly) invert bits in a BitArray?
The only way I made it work is through

a = 1:3 .== 2    # false true false
(!).(a)          # true false true

but this doesn’t look pretty.

1 Like

I think (!).(a) is not that bad. Personally I find map(!, a) easier to read then the broadcast.
Depending on the surrounding code a .== false may or may not be clearer.

2 Likes

This is a slightly better, IMO:

julia> @. !a
3-element BitArray{1}:
  true
 false
  true

Or simply:

julia> .!a
3-element BitArray{1}:
  true
 false
  true
7 Likes

Well, this is the solution :slight_smile:
But this is completely counterintuitive comparing to broadcast of an ordinary function.

@jw3126 map(!, a) also looks good, thank you!

Well, when broadcasting, the dot comes always in front of operators, says here :wink:

1 Like