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
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>>)
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









