How to invert bits in a bit array


#1

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.


#2

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.


#3

This is a slightly better, IMO:

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

#4

Or simply:

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

#5

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!


#6

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