Hi all,
I’ve finally achieved ‘Basic’ status on Elixir Forums, so I have a appropriately basic question
I’m ingesting a mask where each power of 2 aligns to an consecutive index. So, a value of 1, flags the first index, 2 → 2nd, 4-> 3rd, etc. I’m using the default big endianness of the VM, and I’m finding the following:
iex(87)> << 0::1, 1::7 >> = << 1 >>
<<1>>
… whereas I desire something like:
iex(87)> << 1::1, 0::7 >> = some_transform(<< 1 >>)
<<1>>
I’ve mucked around with big and little modifiers on either side of the match operator, but I assume the order of ingest when the bitstring is split is predetermined to be most-significant/lowest-bit to least-significant/highest-bit, low-byte to high-byte, and the input byte will always be in the same bit-order—which all makes sense. I see that there’s :binary
encode
and decode
functions, but once again they only affect byte order, as it should.
I’ve resolved to doing:
(for << bit::1 <- << 1 >> >>, into: [], do: bit)
|> Enum.reverse()
… to reverse the bits so that I can ingest them the way I expect. Though I suspect I’ve missed some obvious easier way, or some erlang function that might be endian-agnostic, and maybe even optimize this away to a trivial assembly operation—am I hoping too much?
Any pointers would be much appreciated Thanks!