在通过网络发送浮点数之前将其移位的原因是什么

What Are The Reasons For Bit Shifting A Float Before Sending It Via A Network

我使用 Unity 和 C# - 在制作多人游戏时,有人告诉我,当涉及到像浮点数这样的值时,我应该在发送它们之前对它们使用位移运算符并反转操作收到。有人告诉我,这不仅允许更大的数值,而且能够保持可能丢失的浮点精度。但是,如果我不必这样做,我不希望每次收到数据包时都进行 运行 这个操作,除非我必须这样做。尽管瓶颈似乎是对接收到的字节的实际解析。特别是在没有消息框架和尝试从字符串移动到字节数组的情况下。 (但那是另一回事了!)

我的问题是:

这些是做手术的正当理由吗?它们是准确的陈述吗? 如果不是,我应该 运行ning 对我的花车进行移位操作吗? 如果我应该这样做,真正的原因是什么?

如有任何补充信息,我们将不胜感激。

我指的资源之一:

来回移动的主要原因 to/from 网络字节顺序是为了对抗 endianness caused problems, mainly to ensure each byte of multi byte values (long, int but also floats) is read and written in the way giving the same results regardless of architecture. This issue can be theoretically ignored if you are sure you are exchanging data between systems using the same endianness, but that's rather bad idea from very beginning as you are simply creating unneded technological debt 并在代码中保留不合理的异常(“这一切都有效,但仅在相同的字节序上。可以去错了吗?”)。

根据您的应用架构,您可以在收到数据包后重写 payload/data,然后在代码中进一步使用该版本。另请注意,您需要在发送数据之前再次对数据进行编码。