ENDIAN_-6ren">
gpt4 book ai didi

Julia 位串函数和字节顺序

转载 作者:行者123 更新时间:2023-12-02 09:09:53 25 4
gpt4 key购买 nike

我在 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)你的足弓。插入 htonntoh如果你解析数据包。

这是因为位串最常用于使用标志的现实检查代码,并且 <<等人按主机字节顺序操作。

关于Julia 位串函数和字节顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53656963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com