# Arbitrary Bit Width Integers

**URL:** <https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499>\
**Category:** Internals & Design\
**Created:** [March 29, 2019, 12:13pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499 "2019-03-29T12:13:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sumit\_Adhikari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumit_adhikari/32/7690_2.png) [@Sumit\_Adhikari](https://discourse.julialang.org/u/Sumit_Adhikari)\
**Post date:** [March 29, 2019, 12:13pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/1 "2019-03-29T12:13:02Z")

</div>

Hi,

First of all I want to thank for making Julia so useful for systems architects to set up initial algorithms. It ia very elegant and very modern.

For me when doing the digital signal processing in HW perspective, I miss arbitrary bit width integers. Like 5 bits signed number or 19 bit unsigned number and associated arithmetic support. It will be wonderful if we Chip architects can have it - will be killer for early algorithm design.

Regards,  
Sumit

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 29, 2019, 12:29pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/2 "2019-03-29T12:29:55Z")

</div>

For tomorrow’s state-of-the-art applications in chip architecture, up to how many bits do you want to have available for these arbitrary bit width integers? Please list the operations you consider to be “associated arithmetic support”, including any exotics.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 29, 2019, 1:03pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/3 "2019-03-29T13:03:11Z")

</div>

You should be able to implement this as a package. Have a look how `Bool` and `BitArray`s work.

PS: my guess is that “HW” means “high wiggle”.

---

<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:** [March 29, 2019, 1:56pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/4 "2019-03-29T13:56:53Z")

</div>

You can look at **[BitIntegers.jl](https://github.com/rfourquet/BitIntegers.jl)**, it’s registered. But it’s not possible to create bits type with a number of bits which is not a multiple of 8.

---

<div class="post-metadata">

**Author:** ![Sumit\_Adhikari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumit_adhikari/32/7690_2.png) [@Sumit\_Adhikari](https://discourse.julialang.org/u/Sumit_Adhikari)\
**Post date:** [March 29, 2019, 8:26pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/5 "2019-03-29T20:26:18Z")

</div>

For any practical purpose bit-width upto 128 should suffice all signal processing and RF architectural needs.

List of operations:

1. Arithmetic: +, -, \*, /
2. Logical: !, ^, & | \>\> \>\>\> \<\<
3. Range Access and write routines.

For further awesomeness:

1. Syntax close to A[N-1:0] will be very interesting, because then no need of range routines. We can access ranges using x = A[5:3] and can modify A[8:1] = 9 ;
2. Data representation with 1’s complement, 2’s complement and Canonical Signed Digit will make you lovable.
3. Optional overflow handling (like: Saturation, Wrap Around) and optional rounding on discarded (LSB) bits (happens due to shifting or division) will make you legends.

Please let me know if you need any further information on this. Will be happy to help.

Regards,  
Sumit

---

<div class="post-metadata">

**Author:** ![Sumit\_Adhikari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumit_adhikari/32/7690_2.png) [@Sumit\_Adhikari](https://discourse.julialang.org/u/Sumit_Adhikari)\
**Post date:** [March 29, 2019, 9:37pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/6 "2019-03-29T21:37:11Z")

</div>

If you want speed, when thousands of these kind of variables will be there, then you will not try that. We have examples of that kind of disasters.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 29, 2019, 10:12pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/7 "2019-03-29T22:12:02Z")

</div>

If a package is created with 256 types like this

1 OnebitsignInteger  
2 OnebitunsignInteger  
.  
.  
.  
255 OneHundredTwentyEightbitsignInteger  
256 OneHundredTwentyEightbitunsignInteger

Will that satisfy your requirement “bit-width up to 128 should suffice”?

---

<div class="post-metadata">

**Author:** ![chrisvwx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvwx/32/45289_2.png) [@chrisvwx](https://discourse.julialang.org/u/chrisvwx)\
**Post date:** [March 29, 2019, 10:12pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/8 "2019-03-29T22:12:57Z")

</div>

If you’re going for arbitrary bit-width integers, why not go for arbitrary fixed-point numbers, which are often used in custom hardware. For example one might have a fixed-point number of width 18 bits, with 5 fractional bits, 1 sign bit, and 12 magnitude bits. There are lotsa things that could be done with this to model the flow of bits through an FPGA or ASIC.

[FixedPointNumbers](https://github.com/JuliaMath/FixedPointNumbers.jl) is not quite this flexible.

---

<div class="post-metadata">

**Author:** ![Sumit\_Adhikari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumit_adhikari/32/7690_2.png) [@Sumit\_Adhikari](https://discourse.julialang.org/u/Sumit_Adhikari)\
**Post date:** [March 30, 2019, 5:27am UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/9 "2019-03-30T05:27:11Z")

</div>

Actually I need generic way to create such variables to write reusable and parametric code. The naming you are suggesting are too unfriendly for that purpose.

---

<div class="post-metadata">

**Author:** ![Sumit\_Adhikari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumit_adhikari/32/7690_2.png) [@Sumit\_Adhikari](https://discourse.julialang.org/u/Sumit_Adhikari)\
**Post date:** [March 30, 2019, 5:46am UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/10 "2019-03-30T05:46:36Z")

</div>

We use fixed point numbers. But they have base 10 and are complex in terms of area and current consumption. What I asked are simple HW types we use everyday which are base 2.

I believe, what I asked more as overflow and rounding feature confused you.

We even use arbitrary bit width floating point numbers.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 30, 2019, 6:32am UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/11 "2019-03-30T06:32:05Z")

</div>

Your willingness to help matters. I am inclined to do this as long as there are other contributors.

It would be great to glean design insight from the community before some of us [IntegerBits.jl](https://github.com/JeffreySarnoff/IntegerBits.jl).

---

<div class="post-metadata">

**Author:** ![Sumit\_Adhikari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sumit_adhikari/32/7690_2.png) [@Sumit\_Adhikari](https://discourse.julialang.org/u/Sumit_Adhikari)\
**Post date:** [March 30, 2019, 7:06am UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/12 "2019-03-30T07:06:16Z")

</div>

Thanks for the proposal.

I am willing to help! I immediately promise you to help with concept and requirements. For coding level contribution, you need to wait till next week when I need to take some additional approvals from my management and legal.

By the way, I saw you already wrote ArbFloat - Thats a pleasant surprise for me.

Let me give you a brief overview on how we do things. We design untimed (no clock or reset, only data flow) algorithms of differ blocks using MATLAB and once when they work correctly, we re write them as timed and detailed architectures using C++/SystemC/SystemC-AMS to create a complete chip model and make the complete thing working also by modelling it’s environment. This working thing is called proof of concept. We then extract out the specs for design implementation.

We want MATLAB to be replaced by Julia.

Please let me know what else do you expect.

Regards,  
Sumit

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [March 30, 2019, 8:36pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/13 "2019-03-30T20:36:39Z")

</div>

are any of these implementations that go above 128 bits treated as `isbits` types?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 30, 2019, 10:15pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/14 "2019-03-30T22:15:50Z")

</div>

```julia
primitive type P512 512 end

julia> isbitstype(P512)
true

struct Signed512
    value::Tuple{Int128,UInt128,UInt128,UInt128}
end

julia> isbitstype(Signed512)
true
```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 30, 2019, 10:34pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/15 "2019-03-30T22:34:33Z")

</div>

Do you have a use for that?

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [March 30, 2019, 10:37pm UTC](https://discourse.julialang.org/t/arbitrary-bit-width-integers/22499/16 "2019-03-30T22:37:38Z")

</div>

yes, I have a use for something like that in [DirectSum.jl](https://github.com/chakravala/DirectSum.jl) and [Grassmann.jl](https://github.com/chakravala/Grassmann.jl), where I would like to generalize the bit depth, at the moment it relies on 64 bit, it would require somewhat significant redesign though.
