Bitmask operations on Bitstrings

Hello evbd,

i’d like to perform bitmask operations on a large Bitstrings. From what i’ve read here and elsewhere all Bitwise-operations band/bor/bnor work on integers only. In erlangs :crypto-moduleis another xor-func, that AFAIK works on binaries, but not on bitstrings. In this thread [ ] i found solutions/workarounds that would involve padding and trimming of bitstrings so that one can use Bitwise.
I could use a Enumerable and MapSets to achive what i want. But i assume keeping anything in a large Bitstring and working with bitmasks should be more efficient.
I tried to iterate a bitstring in 10-bit-steps and expected a bitstring to work, but recieved a integer instead ?
Am i doing samt. wrong here ?

bs1 = 90
for << el::size(10) <- >> do
    IO.puts "|#{el}| is int ? #{is_integer el}"

What would i need to do to consume chunks of a Bitstring that are not a multiple of 8 and therefore Binaries ?

greets Andreas

Well, if you want to do bitwise operations on whole binaries, then you can change binary to integer, do some operations, change it back:

int = :binary.decode_unsigned(binary)
xored = bxor(int, 0xff)
binary = :binary.encode_unsigned(binary)

However if you want to match some bits of the binary, then you can do it directly via pattern matching. For example population count in Elixir can be written as such:

def popcount(""), do: 0
def popcount(<<1::size(1), rest::bitstring>>), do: 1 + popcount(rest)
def popcount(<<0::size(1), rest::bitstring>>), do: popcount(rest)
1 Like

The default type is integer; if you want bitstrings use bits - for << el::bits-size(10) <- >> do

thx to both of you,

@hauleth i knew these exist, but thx anyway.

@al2o3cr that does the trick. I could not find the syntax need. thx again