且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

反序列化功能(字节数组到uint32)

更新时间:2023-11-01 22:43:28

与第二个变量相比,我更喜欢第一个变量。或者,您可以通过使四个局部变量利用各个字节以正确的数量移位来利用并行处理。然后,在最后一行中,您返回b0shifted |。 b1已转移| b2shifted | b3shifted

I prefer your first variant over the second. Or you might exploit parallel processing by having four local variables that take the individual bytes shifted by the correct amount. Then, in the final line you return b0shifted | b1shifted | b2shifted | b3shifted.

无论如何,这都取决于您的编译器。您的第二个变体包含更多的加载/存储操作,因此第一个变体具有更少的抽象操作。

Anyway, it all depends on your compiler. Your second variant contains more load/store operations, so the first variant has fewer abstract operations.

关于可读性,可理解性和清晰度,第一个变体很棒。只要 CHAR_BIT == 8

Concerning readability, understandability and clarity, your first variant is great. It also works on whatever weird platform you are using (endianess, alignment), provided that CHAR_BIT == 8.