suazi

suazi

Separating two integers from the upper/lower bits of a single 32 bit value from little endian

I have a bitstring:

<<3, 0, 0, 0>>

that i’m trying to split into two values like so:

<<chunk::little-unsigned-size(31), is_first_chunk::little-unsigned-size(1)>> = <<3, 0, 0, 0>>

following this specification:

chunk/isFirstChunk (upper 31bits/lowest bit)

“chunk” and “isFirstChunk” are combined into an unsigned 32bit value. Therefore it will be encoded as

uint32_t chunkX

and extracted as

chunk = chunkX >> 1

isFirstChunk = chunkX & 0x1

and I’m expecting both values to be 1 but it’s actually 3 and 0 respectively.

Am I wrong?

Marked As Solved

massimo

massimo

Correct.
Elixir defaults to big endian format when manually crafting binaries.

be aware that most networking protocols are big endian, while almost any common CPU architecture is little endian.

Se if you’re decoding network streams, most probably the will be big endian (most significant byte first)

You can specify endianness in this way

iex(30)> :binary.decode_unsigned(<<3, 0, 0, 0>>, :little)
3
iex(31)> <<chunk::32-little> = <<3, 0, 0, 0>>
iex(32)> chunk
3

encoding

iex(1)> <<3::32-little>>
<<3, 0, 0, 0>>
iex(5)> <<0b0000001::32-little>>
<<1, 0, 0, 0>>
iex(6)> <<0b0000010::32-little>>
<<2, 0, 0, 0>>
iex(7)> <<0b0000011::32-little>>
<<3, 0, 0, 0>>
iex(8)> <<0b0000100::32-little>>
<<4, 0, 0, 0>>

Also Liked

massimo

massimo

it is correct.

<<3, 0, 0, 0>> is a bitstring, encoded as a 4 bytes it is 0x03 0x00 0x00 0x00 and as a 32bit integer it becomes 50331648.

50331648 & 0x01 is zero, because the last byte of the integer is not set (the fourth zero in the four bytes sequence)

0x03 0x00 0x00 0x01 (50331649) would give you 1.

iex(11)> <<chunk::little-unsigned-size(31), is_first_chunk::little-unsigned-size(1)>> = <<3, 0, 0, 1>>
<<3, 0, 0, 1>>
iex(12)> is_first_chunk
1

iex(17)> <<chunkX::32>> = <<3, 0, 0, 1>>
<<3, 0, 0, 1>>
iex(18)> require Bitwise
Bitwise
iex(19)> Bitwise.&&&(chunkX, 0x01)      
1
iex(20)> chunkX
50331649

iex(21)> <<chunkX::32>> = <<3, 0, 0, 0>>
<<3, 0, 0, 0>>
iex(22)> Bitwise.&&&(chunkX, 0x01)      
0
iex(23)> chunkX                         
50331648

iex(24)> Bitwise.>>>(chunkX, 1)   
25165824 <-- chunk

EDIT: you can convert the integer back to a bitstring with

iex(25)> :binary.encode_unsigned(50331648)
<<3, 0, 0, 0>>
iex(26)> :binary.encode_unsigned(50331649)
<<3, 0, 0, 1>>
suazi

suazi

Yup, I’m workin on VelocyStream (ArangoDB).

With your insight, it became clear. To encode for an outgoing chunk:

chunk_x =
  <<chunk::31, is_first_chunk::1>>
  |> :binary.decode_unsigned(:little)
  |> :binary.encode_unsigned(:big)

and decode from an incoming chunk:

<<chunk::31, is_first_chunk::1>> =
  chunk_x
  |> :binary.decode_unsigned(:big)
  |> :binary.encode_unsigned(:little)

I really appreciate it, thank you.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement