# Primitive type with 2 bits

**URL:** <https://discourse.julialang.org/t/primitive-type-with-2-bits/85095>\
**Category:** General Usage\
**Created:** [August 1, 2022, 10:11am UTC](https://discourse.julialang.org/t/primitive-type-with-2-bits/85095 "2022-08-01T10:11:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_231578](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user_231578/32/24170_2.png) [@user\_231578](https://discourse.julialang.org/u/user_231578)\
**Post date:** [August 1, 2022, 10:11am UTC](https://discourse.julialang.org/t/primitive-type-with-2-bits/85095/1 "2022-08-01T10:11:03Z")

</div>

I have a very large matrix (with millions of rows) which obviously uses a lot of memory. Each of its entry it is either 0, 1 or 2. At the moment, the matrix has type Array{Int8,2} but 2 bits per entry would suffice, and would also reduce the memory allocation.

I tried to define a new type:

primitive type UInt2 \<: Unsigned 2 end

but it throws an error. Is 8 bits is the minimum one can use?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [August 1, 2022, 10:22am UTC](https://discourse.julialang.org/t/primitive-type-with-2-bits/85095/2 "2022-08-01T10:22:45Z")

</div>

> [@user\_231578](#):
>
> Is 8 bits is the minimum one can use?

Indeed. A `DiBitVector` might be useful to you: [DiBitVector · DataStructures.jl](https://juliacollections.github.io/DataStructures.jl/latest/dibit_vector/)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 1, 2022, 10:30am UTC](https://discourse.julialang.org/t/primitive-type-with-2-bits/85095/3 "2022-08-01T10:30:56Z")

</div>

There’s also `BitArray`s: [https://docs.julialang.org/en/v1/base/arrays/#Base.BitArray](https://docs.julialang.org/en/v1/base/arrays/#Base.BitArray)

edit: oops, I misread the OP, thought they just needed two values instead of three.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 1, 2022, 1:59pm UTC](https://discourse.julialang.org/t/primitive-type-with-2-bits/85095/4 "2022-08-01T13:59:13Z")

</div>

Yes, 8 bits is the minimum one can use for a primitive type.

However, you can still implement bit-packed data structures like `BitArray` or `DBitVector` as linked above. The way these works is to have arrays of e.g. `UInt64` “chunks” (64-bit quantities) under the hood, but to provide accessor functions (e.g. `getindex`) that extract individual 1-bit or 2-bit elements from the bits of these “chunks”, respectively.

---

<div class="post-metadata">

**Author:** ![user\_231578](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user_231578/32/24170_2.png) [@user\_231578](https://discourse.julialang.org/u/user_231578)\
**Post date:** [August 2, 2022, 2:49pm UTC](https://discourse.julialang.org/t/primitive-type-with-2-bits/85095/5 "2022-08-02T14:49:55Z")

</div>

This ended up being the best solution for me. I did not want to define the matrix using a vector of DiBiVectors, and I preferred defining a BitArray matrix with twice the number of columns, storing the 2 bits information I needed in each 1x2 block of the matrix
