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?

1 Like

Perhaps using Binary.<<</2.

Something like x = ogf <<< 10 ||| ocf; <<x::little-16>>.

That way you don’t have to pack just for immediate unpacking.

Maybe you can even inline x into the binary, I’m never sure what’s possible within a binary literal, I use them to rarely for creating values.

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>>)
2 Likes

Trimming would have happened in the little-16 part. At least for ogf, but indeed I’ve forgotten about ocf (or did I just assume that data has been sanitizer already?)

Interesting. Where are the docs for that? I had no idea that was a thing.

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…

2 Likes