# Arithmetic using binary numbers

**URL:** <https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823>\
**Category:** Numerics\
**Tags:** numbers\
**Created:** [September 27, 2021, 4:57pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823 "2021-09-27T16:57:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaidy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaidy/32/20310_2.png) [@Jaidy](https://discourse.julialang.org/u/Jaidy)\
**Post date:** [September 27, 2021, 4:57pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/1 "2021-09-27T16:57:55Z")

</div>

I have huge matrices each entry of which is a 2-bit binary number. If I were to load them and convert to any of the Floats precisions, the memory required to load will be at least half a terabyte. Can Julia do arithmetic on (2-bit) binary numbers?

---

<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:** [September 27, 2021, 5:38pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/2 "2021-09-27T17:38:35Z")

</div>

> [@Jaidy](#):
>
> Can Julia do arithmetic on (2-bit) binary numbers?

Basically yes. You can always implement this yourself using bitfield operations, or use e.g. [GaloisFields.jl](https://github.com/tkluck/GaloisFields.jl) (though you still may need to load them from your array yourself — the smallest type that Julia can store in an array is `UInt8`, 8 bits, but you can think of this as a packed representation of 4 2-bit numbers that you can extract using bit operations).

There is no “built-in” array of 2-bit numbers in Julia, but that’s fine — user-written code for this can be just as efficient as anything “built-in” could be.

What operations do you want to perform on your matrices?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [September 27, 2021, 6:32pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/3 "2021-09-27T18:32:31Z")

</div>

> the smallest type that Julia can store in an array is `UInt8`

Unless you count `BitArray`

---

<div class="post-metadata">

**Author:** ![Jaidy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaidy/32/20310_2.png) [@Jaidy](https://discourse.julialang.org/u/Jaidy)\
**Post date:** [September 27, 2021, 7:03pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/4 "2021-09-27T19:03:19Z")

</div>

I’m looking to do linear algebra like matrix vector product, solving a linear system, matrix decomposition/factorization operations (also basic arithmetic of course)

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 27, 2021, 7:33pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/5 "2021-09-27T19:33:59Z")

</div>

Assuming you can pack your initial values into two bits and provide suitable methods for how you represent them, you’re likely to generate values that are no longer two-bit numbers. Unless your precision requirements are extremely low (so low as to be unuseful), you’ll need to store those results in much larger sized numbers.

---

<div class="post-metadata">

**Author:** ![Jaidy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaidy/32/20310_2.png) [@Jaidy](https://discourse.julialang.org/u/Jaidy)\
**Post date:** [September 27, 2021, 8:23pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/6 "2021-09-27T20:23:18Z")

</div>

That is certainly true. But then perhaps I’m wondering if I can vary the number of bits to be used for variables arbitrarily

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 27, 2021, 8:37pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/7 "2021-09-27T20:37:52Z")

</div>

There’s [Integers and Floating-Point Numbers · The Julia Language](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic), but that is used to get more precision, not less.

---

<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:** [September 27, 2021, 10:19pm UTC](https://discourse.julialang.org/t/arithmetic-using-binary-numbers/68823/8 "2021-09-27T22:19:15Z")

</div>

> [@Jaidy](#):
>
> That is certainly true. But then perhaps I’m wondering if I can vary the number of bits to be used for variables arbitrarily

I doubt variable precision will help you — you will quickly need the full precision required for your problem (which depends on the condition number of your matrix).

(When you originally posted, I thought you meant some kind of finite-field arithmetic, but it sounds like you are doing ordinary real arithmetic, in which case you generally need floating-point numbers with decent precision for the results and intermediate values even if the input matrix entries can be represented with 2 bits.)

Maybe there is some other structure you can exploit. For example, if your huge matrix is mostly zeros (sparse), then you can use sparse-matrix methods.
