function bools_to_bytes(vec::Vector{Bool})
vec = extend_bools(vec)
n = length(vec)
n == 0 && return [zero(UInt8)]
@assert rem(n, 8) == 0
nbytes = div(n, 8)
return bools_to_nbytes(vec, nbytes)
end
function bools_to_nbytes(vec::Vector{Bool}, nbytes::Int)
result = zeros(UInt8, nbytes) # preallocate result space
for byteidx in 1:nbytes
vecstart = 8*(byteidx-1) + 1
boolbits = UInt8.(vec[vecstart:(vecstart+7)])
for i in 1:8
boolbits[i] = boolbits[i] << (8-i)
end
newbyte = reduce(|, boolbits)
push!(result, newbyte)
end
return result[length(result)-nbytes+1:end]
end
function extend_bools(bools::Vector{Bool})
n = length(bools)
rm = rem(n, 8)
while 0 < rm < 8
push!(bools, false)
rm += 1
end
return bools
end
hope this helps