# How to invert bits in a bit array

**URL:** https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400
**Category:** New to Julia
**Created:** [November 11, 2018, 11:06am UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400 "2018-11-11T11:06:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)
#### Post date: [November 11, 2018, 11:06am UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400/1 "2018-11-11T11:06:19Z")

</div>

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

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

```

but this doesn’t look pretty.

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [November 11, 2018, 11:18am UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400/2 "2018-11-11T11:18:40Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [November 11, 2018, 11:52am UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400/3 "2018-11-11T11:52:00Z")

</div>

This is a slightly better, IMO:

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

```

---

<div class="post-metadata">

### Author: ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)
#### Post date: [November 11, 2018, 12:03pm UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400/4 "2018-11-11T12:03:00Z")

</div>

Or simply:

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

```

---

<div class="post-metadata">

### Author: ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)
#### Post date: [November 11, 2018, 12:49pm UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400/5 "2018-11-11T12:49:56Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)
#### Post date: [November 11, 2018, 1:17pm UTC](https://discourse.julialang.org/t/how-to-invert-bits-in-a-bit-array/17400/6 "2018-11-11T13:17:47Z")

</div>

Well, when broadcasting, the dot comes always in front of operators, says [here](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators-1) 😉
