achan1989

achan1989

Create little-endian binary from x::6 y::10 integer parts

I want tidy code that creates an opcode from two pieces, and encodes it as a little endian binary. Where “the OGF occupies the upper 6 bits of the Opcode, and the OCF occupies the remaining 10 bits.” This code is almost what I want, except that the resulting binary is big endian:

def opcode_bin(ogf, ocf), do: <<ogf::6, ocf::10>>

<<16, 1>> = opcode_bin(4, 1)
# I want <<1, 16>>

The following works, but the intermediate step isn’t very elegant:

def opcode_bin(ogf, ocf) do
  <<opcode::16>> = <<ogf::6, ocf::10>>
  <<opcode::little-16>>
end

Is there a nicer way to accomplish this, maybe by specifying that I want little-endian output directly?

Most Liked

aziz

aziz

Careful with the Bitwise operators. ogf <<< 10 would only shift and not mask like <<ogf::6>>. Masking cuts off the extra bits. So as an expression it can be:

<<(ogf &&& 0x3F) <<< 10 ||| (ocf &&& 0x3FF)::little-16>>

I guess you’d agree that’s hardly more readable. Alternatively you could create a handy swap16 function that swaps its bytes. Then you can have everything in one line and the intent is clear as day.

def opcode_bin(ogf, ocf), do: swap16(<<ogf::6, ocf::10>>)
NobbZ

NobbZ

Sorry, my fault, it was Bitwise, not Binary… That is the kind of stuff that happens when one replies to technical topics while waiting for the train home…

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
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
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
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

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
rms.mrcs
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
9mm
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

We're in Beta

About us Mission Statement