# Reducing BitArray with xor

**URL:** <https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354>\
**Category:** General Usage\
**Created:** [February 1, 2019, 1:10pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354 "2019-02-01T13:10:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethomag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethomag/32/421_2.png) [@ethomag](https://discourse.julialang.org/u/ethomag)\
**Post date:** [February 1, 2019, 1:10pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354/1 "2019-02-01T13:10:01Z")

</div>

I am trying to xor a number of `BitArray{1}` vectors

```julia
julia> a=BitArray([1 1;1 0])
 2×2 BitArray{2}:
  true true
  true false
julia> reduce(⊻, a, dims=1)
ERROR: MethodError: no method matching reducedim_init(::typeof(identity), ::typeof(xor), ::BitArray{2}, ::Int64)

```

However, other similar operators works fine:

```julia
julia> reduce(&, a, dims=1)
1×2 BitArray{2}:
 true false

julia> reduce(|, a, dims=1)
1×2 BitArray{2}:
 true true

```

Is there something missing in Base regarding `xor` ?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 1, 2019, 1:32pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354/2 "2019-02-01T13:32:17Z")

</div>

See the docstring of `reduce` (emphasis mine):

> ```
> reduce(f, A; dims=:, [init])
> 
> ```
> 
> Reduce 2-argument function `f` along dimensions of `A`. `dims` is a vector specifying the  
> dimensions to reduce, and the keyword argument `init` is the initial value to use in the  
> reductions. **For `+`, `*`, `max` and `min` the `init` argument is optional.**

That said, the error message could be nicer. And there is no reason it could not be extended to `xor` I guess. Perhaps open an issue?

---

<div class="post-metadata">

**Author:** ![ethomag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethomag/32/421_2.png) [@ethomag](https://discourse.julialang.org/u/ethomag)\
**Post date:** [February 1, 2019, 1:38pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354/3 "2019-02-01T13:38:37Z")

</div>

Thanks @Tamas_Papp. I tried this, but still no luck …

```julia
julia> reduce(⊻, a, dims=1, init=BitVector([0,0]))
ERROR: MethodError: no method matching xor(::BitArray{1}, ::Bool)

```

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [February 1, 2019, 1:44pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354/4 "2019-02-01T13:44:03Z")

</div>

It works if you initialize it with just a bool, e.g.:

```julia
julia> a=BitArray([1 1;1 0])
2×2 BitArray{2}:
 true true
 true false
julia> reduce(⊻, a, dims=1, init=false)
1×2 BitArray{2}:
 false true

```

(Granted I found that quite suprising as your approach seemed intuitive to me too…)  
Cheers

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 1, 2019, 1:56pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354/5 "2019-02-01T13:56:29Z")

</div>

I guess a PR clarifying the docs would be very useful.

---

<div class="post-metadata">

**Author:** ![ok\_ok](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ok_ok/32/15450_2.png) [@ok\_ok](https://discourse.julialang.org/u/ok_ok)\
**Post date:** [June 6, 2020, 3:26pm UTC](https://discourse.julialang.org/t/reducing-bitarray-with-xor/20354/6 "2020-06-06T15:26:37Z")

</div>

the solution above stopped working for me somehow - maybe this is usefull for anyone in the future 😛

```julia
function (^)(l::BitArray, r::BitArray)
   xorArray = BitArray(fill(0, (1,consts.sn)))

   for (index, leftValue) = enumerate(l)
       if leftValue && !r[index] || !leftValue && r[index]
           xorArray[index] = 1
       end
   end

   return xorArray
end

```
