Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

&-ing two BitArrays in Julia?

Tags:

julia

Using Julia 1.5.3 and Julia 1.6.0 neither versions seems to support & for BitArrays.

I have two BitArrays like for instance

x = BitArray([1,0,1])

and

y=BitArray([0,0,1])

and wish to intersect them to find:

x&y=BitArray([0,0,1]) 

but the operator & does not seem to support BitArrays and using .* seems to be very time consuming.

Does anyone know a good method for finding the intersection of two bit arrays in Julia?

like image 648
Kristine Avatar asked Nov 15 '25 07:11

Kristine


1 Answers

& works for scalar values, while you are applying them to arrays. When applying scalar operators (or functions) to an array, you should use 'broadcasting', which you can do by adding a dot to the operator:

jl> x .& y
3-element BitVector:
 0
 0
 1

BTW, I cannot see any timing difference between .* and .&. In fact it seems that * just calls &.

What sort of performance are you seeing?

jl> using BenchmarkTools

jl> @btime $x .* $y;
  48.479 ns (2 allocations: 128 bytes)

jl> @btime $x .& $y;
  48.426 ns (2 allocations: 128 bytes)
like image 145
DNF Avatar answered Nov 17 '25 20:11

DNF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!