Sebb

Sebb

Bitstring generator gives odd result for 6 and 7 bits

I understand these results:

iex(1)> for <<b::1 <- <<0xff::16>> >>, do: b
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
iex(2)> for <<b::2 <- <<0xff::16>> >>, do: b
[0, 0, 0, 0, 3, 3, 3, 3]
iex(3)> for <<b::3 <- <<0xff::16>> >>, do: b
[0, 0, 1, 7, 7]
iex(4)> for <<b::4 <- <<0xff::16>> >>, do: b
[0, 0, 15, 15]
iex(5)> for <<b::5 <- <<0xff::16>> >>, do: b
[0, 3, 31]

but here I’d expect [3, 63]

iex(6)> for <<b::6 <- <<0xff::16>> >>, do: b
[0, 15]

… and [1, 127] here

iex(7)> for <<b::7 <- <<0xff::16>> >>, do: b
[0, 63]

this makes sense again

iex(8)> for <<b::8 <- <<0xff::16>> >>, do: b
[0, 255]

Marked As Solved

zevv

zevv

[0, 15] is to be expected:

0xff::16 maps to [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]. Grouping this int groups of 6 bits will give you:

  • [0, 0, 0, 0, 0, 0], which equals 0
  • [0, 0, 1, 1, 1, 1], which equals 15 and
  • [1, 1, 1, 1], which is too small and discarded.

Also Liked

Eiji

Eiji

Here is a code example which produces data that you expect:

defmodule Example do
  def sample(bitstring, size) do
    # simply calculate all sizes
    bit_size = bit_size(bitstring)
    extra_size = rem(bit_size, size)
    target_size = bit_size - extra_size
    # using pattern matching and above variables
    # we are ignoring x extra 0 bits at start of bitstring
    # where x here is calculated as extra_size
    <<0::size(extra_size), target::size(target_size)>> = bitstring
    # original loop but more generic and with target data
    for <<(item::size(size) <- <<target::size(target_size)>>)>>, do: item
  end
end

iex> Example.sample(<<0xFF::16>>, 1)
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
iex> Example.sample(<<0xFF::16>>, 2)
[0, 0, 0, 0, 3, 3, 3, 3]
iex> Example.sample(<<0xFF::16>>, 3)
[0, 0, 3, 7, 7]
iex> Example.sample(<<0xFF::16>>, 4)
[0, 0, 15, 15]
iex> Example.sample(<<0xFF::16>>, 5)
[0, 7, 31]
iex> Example.sample(<<0xFF::16>>, 6)
[3, 63]
iex> Example.sample(<<0xFF::16>>, 7)
[1, 127]
iex> Example.sample(<<0xFF::16>>, 8)
[0, 255]

Note: I’m not sure what you are trying to do with it, so you may want to also use a case condition when extra bits are not 0, for example <<0xFFFF::16>>.

Helpful resources:

  1. [Guard] bit_size/1 - Returns an integer which is the size in bits of bitstring.
  2. [Guard] rem/2 - Computes the remainder of an integer division.
  3. [Special form] <<>>/1 - Defines a new bitstring.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement