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

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement