我在 Windows 8.1 下运行 Julia 1.0.2。
以下内容让我相信 Julia 以“小端”方式对待我的机器:
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.
基于以上,下面的位串示例对我来说很有意义。两者都有最不重要的字节在前,在左边,正如我对小端字节顺序所期望的那样:
julia> bitstring(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"
julia> bitstring(Char(1))
"00000001000000000000000000000000"
然而,下面的例子似乎是大端顺序,最低有效字节在右边:
julia> bitstring(1)
"0000000000000000000000000000000000000000000000000000000000000001"
我糊涂了吗?有什么建议或解释吗?
最佳答案
bitstring
关心主机字节顺序。换句话说,
julia> bitstring(1)
"0000000000000000000000000000000000000000000000000000000000000001"
是机器无关的,但是
julia> bitstring(hton(1))
"0000000100000000000000000000000000000000000000000000000000000000"
反射(reflect)你的足弓。插入 hton
和 ntoh
如果你解析数据包。
这是因为位串最常用于使用标志的现实检查代码,并且 <<
等人按主机字节顺序操作。
关于Julia 位串函数和字节顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53656963/