I’m converting between two representations of arrays of bits. One is a vector storing 1 bit per element. The function ‘pack_bits’ will pack this into N bits per element. The function ‘unpack_bits’ will convert in the other direction.

I threw together some primitive code for pack_bits, but I’m guessing more elegant solutions are already available?

For example:

```
function pack_bits(inp::Vector{in_t}, bits_per_word::Integer, dir::Integer, out_t=Integer) where {in_t<:Integer}
if bits_per_word > sizeof(out_t) * 8
jlerror("bits_per_word > sizeof(type)*8")
end
if (length(inp) % bits_per_word != 0)
jlerror("pack_bits: size % bits_per_sym != 0")
end
out = Vector{out_t}(Int(length(inp)/bits_per_word))
for o = 1:length(out)
ret = 0
if dir > 0
for b = 0:bits_per_word-1
ret |= (inp[(o-1)*bits_per_word+b+1]&1) << b
end
out[o] = ret
else
for b = 0:bits_per_word-1
ret |= (inp[(o-1)*bits_per_word+b+1]&1) << (bits_per_word - b - 1)
end
out[o] = ret
end
end
return out
end
u = Vector{Int32}([0,0,0,1,1,0,1,1])
Pack_bits.pack_bits(u, 2, -1, Int32)
4-element Array{Int32,1}:
0
1
2
3
```