How to Invert a Bit Array in Julia
Code 1
It’s quite simple, but a common mistake is to treat the negation operators !
and ~
not as unary operators but as functions, and use !.
or ~.
. They should be written as .!
or .~
instead.
julia> a = rand(1,10) .< 0.5
1×10 BitMatrix:
1 1 0 0 1 0 1 0 0 0
julia> .!(a)
1×10 BitMatrix:
0 0 1 1 0 1 0 1 1 1
julia> .~(a)
1×10 BitMatrix:
0 0 1 1 0 1 0 1 1 1
Environment
- OS: Windows
- julia: v1.6.0