Search code examples
juliabitarray

Julia BitArray with 128 Bits


I need a Julia BitArray-like object that can encode more than 64 bits, say 128 bits. Does a simple replacement of UInt64 with UInt128 in bitarray.jl work?


Solution

  • Based on the information in your comment, the existing BitArray would itself serve your needs. Note that BitArray uses UInt64s internally, but that's not a limitation on the size of the array - it actually stores the bits as a Vector of UInt64s, so there's no special size limitation. You can create a 5x5x5 BitArray with no problem.

    julia> b = BitArray(undef, 5, 5, 5);
    
    julia> b .= 0;
    
    julia> b[3, 5, 5] = 1
    1
    
    julia> b[3, :, :]
    5×5 BitMatrix:
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  1
    

    Maybe this part of the documentation threw you off:

    BitArrays pack up to 64 values into every 8 bytes, resulting in an 8x space efficiency over Array{Bool, N} and allowing some operations to work on 64 values at once.

    but that's talking about internal implementation details. BitArrays are not limited to 8 bytes, so they're not limited to having just 64 values in them either.

    Creating a new type of bit array using UInt128s would likely not be optimized, and is unnecessary anyway.