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
[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.
3
Also Liked
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:
- [Guard] bit_size/1 - Returns an integer which is the size in bits of bitstring.
- [Guard] rem/2 - Computes the remainder of an integer division.
- [Special form] <<>>/1 - Defines a new bitstring.
1
Popular in Questions
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
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
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
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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
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
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
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
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
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
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
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
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







