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
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
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
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New







