Julia BitArray Tutorial
last modified November 18, 2026
A bit array is an array of boolean values that stores each value in a
single bit instead of a whole byte. Julia provides this storage through the
BitArray family of types, which behave like ordinary arrays but use
far less memory for large collections of true and false values.
This tutorial covers creating bit arrays, applying logical operations, measuring the memory savings, indexing, and counting. The examples were verified with Julia 1.13.4.
Creating Bit Arrays
The falses and trues functions create bit arrays filled
with false and true. They take the dimensions as
arguments and return a compact boolean array.
a = falses(3) b = trues(3) println(a) println(b) println(typeof(a))
Both values are one-dimensional BitVector objects. Printing a bit
vector uses a compact form: Bool[0, 0, 0] means three
false entries, while Bool[1, 1, 1] means three
true entries. The vector behaves like a normal Array
of Bool in almost every respect.
$ julia main.jl Bool[0, 0, 0] Bool[1, 1, 1] BitVector
BitVector and BitMatrix
A one-dimensional bit array is a BitVector and a two-dimensional
one is a BitMatrix; both are concrete forms of
BitArray. The BitVector(...) constructor converts an
existing collection of booleans, and falses accepts more than one
dimension.
v = BitVector([true, false, true]) m = falses(2, 3) println(typeof(v)) println(typeof(m)) println(m)
Converting the three-element Bool array produces a
BitVector. Calling falses(2, 3) produces a
BitMatrix with two rows and three columns, displayed with the
same compact Bool[...] notation used for vectors.
$ julia main.jl BitVector BitMatrix Bool[0 0 0; 0 0 0]
Logical Operations
Because bit arrays store booleans, the logical operators work element by
element. The dot prefix (.) is broadcasting: it applies
the operator to each pair of elements rather than to the array as a whole. The
operators are .& (and), .| (or),
.⊻ (exclusive or), and .! (not).
a = BitVector([true, false, true, false]) b = BitVector([true, true, false, false]) println(a .& b) println(a .| b) println(.!a) println(a .⊻ b)
Each operation returns a new BitVector. The .&
result has a single true where both inputs are true;
.| is true where at least one input is true; .!
inverts every element; and .⊻ is true where the two inputs differ.
Broadcasting keeps the compact bit storage in the result.
$ julia main.jl Bool[1, 0, 0, 0] Bool[1, 1, 1, 0] Bool[0, 1, 0, 1] Bool[0, 1, 1, 0]
Building a BitVector from an Iterable
Any iterable of booleans, including a generator expression, can be turned into
a bit vector with the BitVector constructor. A generator describes
how to produce values lazily instead of building an intermediate array.
v = BitVector([i % 2 == 0 for i in 1:6]) println(typeof(v)) println(v)
The generator yields true for even numbers and
false for odd ones, so the result alternates between zero and one.
Some older references mention a bitvector helper function; in
Julia 1.13 the standard way to build a bit vector from an iterable is the
BitVector(itr) constructor shown here.
$ julia main.jl BitVector Bool[0, 1, 0, 1, 0, 1]
Memory Efficiency
The reason to choose a bit array is storage. The sizeof function
reports how many bytes a value occupies, which makes the difference between a
bit array and a normal boolean array easy to see.
b = falses(1000) bv = Bool[false for _ in 1:1000] println(sizeof(b)) println(sizeof(bv)) println(typeof(b))
The ordinary Vector{Bool} needs one byte per entry, so one
thousand values occupy one thousand bytes. The BitVector packs
eight values into each byte and reports 128 bytes here, roughly eight times
less. For small arrays the difference is negligible; for millions of flags it
is substantial.
$ julia main.jl 128 1000 BitVector
Indexing and Modifying
Bit arrays use the same one-based indexing and assignment syntax as ordinary
arrays. Reading an element gives a Bool, and assigning to a
position updates the packed storage in place.
v = BitVector([true, false, true, true, false]) println(v[1]) println(v[3]) v[2] = true println(v) println(count(v)) println(count(!, v))
Positions one and three are true. After setting position two to
true, four of the five entries are set. The count
function returns how many elements satisfy a condition; called on the vector
it counts the true values, and called with the !
function it counts the false ones.
$ julia main.jl true true Bool[1, 1, 1, 1, 0] 4 1
Counting, any, all and sum
Several reduction functions work directly on bit arrays and return a single answer. They are useful for answering questions about a whole set of flags at once.
v = BitVector([true, false, true]) println(any(v)) println(all(v)) println(sum(v))
The any function is true if at least one entry is set, while
all is true only if every entry is set. The sum
function treats true as one and false as zero, so it
returns the number of set bits. Here two of the three entries are true.
$ julia main.jl true false 2
Converting to a Vector of Bool
When a plain byte-per-element array is required, collect converts
a bit vector into a Vector{Bool}. This is the reverse of the
conversion shown earlier.
b = BitVector([true, false, true]) v = collect(b) println(typeof(v)) println(v)
The result is a standard Vector{Bool} with the same elements. Use
this when interoperating with code that specifically requires that concrete
type; otherwise keeping the BitVector saves memory.
$ julia main.jl
Vector{Bool}
Bool[1, 0, 1]
Best Practices for BitArrays
A few guidelines help when deciding where bit arrays are appropriate.
- Use them for large flag sets: the memory saving matters only when there are many booleans.
- Broadcast logical operators: write
.&and.|so the operation is applied element by element. - Prefer
countover manual loops: it expresses the intent clearly and is optimized for bit arrays. - Convert only when necessary: collect into a
Vector{Bool}only for APIs that demand that exact type.
Source
Julia Bit Arrays documentation
In this article we have worked with Julia bit arrays: creating
BitVector and BitMatrix values, broadcasting logical
operations, comparing memory use, indexing and counting. Ordinary arrays are
covered in Julia arrays, boolean values in
Julia data types, and the
. broadcast syntax in Julia
operators.
Author
List all Julia tutorials.