At7heb

At7heb

Extracting 15-bit integer values from binary with pattern matching

I have time deltas stored in a file in two formats:
0xxxxxxx
1yyyyyyyyyyyyyyy

In the first format, the time delta is the unsigned 7 bit field represented by xxxxxxx. In the second format, the time delta is the signed 15 bit field represented by yyyyyyyyyyyyyyy

pattern matching to extract the values works for the 7 bit unsigned case:

iex(12)> <<0::1, num::size(7)-unsigned-integer-little>> = <<2>>; num
2

But for the intuitive extension to the 15-bit signed case, it isn’t so obvious that it is working correctly:

iex(15)> <<1::1, num::size(15)-signed-integer-little>> = <<255,255>>; num
-1

That’s reasonable. However:

iex(14)> <<1::1, num::size(15)-signed-integer-little>> = <<254,255>>; num
-3

The value of the 15 bit signed field is -2, not -3.

I’ve been searching for the documentation fruitlessly, so a pointer to the place where I can read the fine documentation, or else just the answer, would be appreciated.

thanks…

Most Liked

At7heb

At7heb

Thank you for the pointer to the erlang documentation. That looks like the syntax, with the : (erlang) vs. :: (elixir) difference.

Looking at that documentation inspired me to ask what elixir would construct, given the same layout as in the pattern, so I entered the following into iex. It seems that the affected bits are the least significant bits of the first byte and the most significant bits of the second byte.

Without a specification of how to place a 1 bit field and a 15-bit little endian field into two bytes, I can’t say I know how to write the elixir pattern,

I’m going to consider this as closed. (Not sure how to/if I can close a topic in this forum.) Thank you for your insights.

iex(57)> Enum.map(-16..16, &(<<0::1, &1::15-signed-little>> <> <<0>> <> <<&1::8>>))
[
  <<120, 127, 0, 240>>,
  <<120, 255, 0, 241>>,
  <<121, 127, 0, 242>>,
  <<121, 255, 0, 243>>,
  <<122, 127, 0, 244>>,
  <<122, 255, 0, 245>>,
  <<123, 127, 0, 246>>,
  <<123, 255, 0, 247>>,
  <<124, 127, 0, 248>>,
  <<124, 255, 0, 249>>,
  <<125, 127, 0, 250>>,
  <<125, 255, 0, 251>>,
  <<126, 127, 0, 252>>,
  <<126, 255, 0, 253>>,
  <<127, 127, 0, 254>>,
  <<127, 255, 0, 255>>,
  <<0, 0, 0, 0>>,
  <<0, 128, 0, 1>>,
  <<1, 0, 0, 2>>,
  <<1, 128, 0, 3>>,
  <<2, 0, 0, 4>>,
  <<2, 128, 0, 5>>,
  <<3, 0, 0, 6>>,
  <<3, 128, 0, 7>>,
  <<4, 0, 0, 8>>,
  <<4, 128, 0, 9>>,
  <<5, 0, 0, 10>>,
  <<5, 128, 0, 11>>,
  <<6, 0, 0, 12>>,
  <<6, 128, 0, 13>>,
  <<7, 0, 0, 14>>,
  <<7, 128, 0, 15>>,
  <<8, 0, 0, 16>>
]

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics 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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement