Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia bitstring function and byte order

Tags:

julia

I am running Julia 1.0.2 under Windows 8.1.

The following leads me to believe Julia treats my machine in "little-endian" fashion:

julia> VERSION
v"1.0.2"

julia> ENDIAN_BOM
0x04030201

help?> ENDIAN_BOM
search: ENDIAN_BOM

  ENDIAN_BOM

  The 32-bit byte-order-mark indicates the native byte order of the host machine. Little-endian
  machines will contain the value 0x04030201. Big-endian machines will contain the value
  0x01020304.

Based on above, the bitstring examples below make sense to me. Both have the least significant byte first, on the left, as I would expect for little-endian byte order:

julia> bitstring(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"

julia> bitstring(Char(1))
"00000001000000000000000000000000"

However, the following example seems to be in big-endian order, the least significant byte is on the right:

julia> bitstring(1)
"0000000000000000000000000000000000000000000000000000000000000001"

Am I confused? Any suggestions or explanations?

like image 602
Julia Learner Avatar asked Oct 28 '25 03:10

Julia Learner


2 Answers

bitstring cares about host byte order. In other words, julia> bitstring(1) "0000000000000000000000000000000000000000000000000000000000000001" is machine-independent, but julia> bitstring(hton(1)) "0000000100000000000000000000000000000000000000000000000000000000" reflects your arch. Insert hton and ntoh if you parse packets.

This is because bitstring is most used for reality-checking code that uses flags, and << et al operate on host byte order.

like image 121
J. Doe Avatar answered Oct 30 '25 14:10

J. Doe


In your question there are two separate issues.

Char representation is a custom design decision in Julia; the approach is that UTF-8 representation of a character is filled with 0s on the right side to get 4 bytes (UInt32); you can see how the convesion happens e.g. in Char(u::UInt32) method definition.

For 1.0 and 1 you can see what are their little and big endian representations using htol and hton functions and you get:

julia> bitstring(htol(1))
"0000000000000000000000000000000000000000000000000000000000000001"

julia> bitstring(hton(1))
"0000000100000000000000000000000000000000000000000000000000000000"

julia> bitstring(htol(1.0))
"0011111111110000000000000000000000000000000000000000000000000000"

julia> bitstring(hton(1.0))
"0000000000000000000000000000000000000000000000001111000000111111"

and all is consistent.

EDIT: see the explanation in the other answer what bitstring exactly does as it is relevant.

like image 21
Bogumił Kamiński Avatar answered Oct 30 '25 14:10

Bogumił Kamiński