简单的位操作的小端整数,以big-endian机器? [英] Simple bitwise manipulation for little-endian integer, in big-endian machine?

查看:251
本文介绍了简单的位操作的小端整数,以big-endian机器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关特定需要我建立一个4字节整数出四单字节字符,使用没有什么太特别的(​​在我的小端平台):

For a specific need I am building a four byte integer out of four one byte chars, using nothing too special (on my little endian platform):

    return (( v1 << 24) | (v2 << 16) | (v3 << 8) | v4);

据我所知,存储在大端机器整数看起来像 AB BC CD DE 而不是 DE CD BC AB 字节序不大,虽然它会完全影响我的操作,我将转向不正确,或者它只会导致存储在反向,需要逆转?

I am aware that an integer stored in a big endian machine would look like AB BC CD DE instead of DE CD BC AB of little endianness, although would it affect the my operation completely in that I will be shifting incorrectly, or will it just cause a correct result that is stored in reverse and needs to be reversed?

我不知道是否要创建此函数做的第二个版本(目前未知)位操作的大端机器,或者可能使用ntonl相关的功能,我不清楚如何将知道我的号码是以正确的顺序或没有。

I was wondering whether to create a second version of this function to do (yet unknown) bit manipulation for a big-endian machine, or possibly to use ntonl related function which I am unclear of how that would know if my number is in correct order or not.

什么是您的建议,以确保兼容性,牢记我确实需要形成整数这种方式?

What would be your suggestion to ensure compatibility, keeping in mind I do need to form integers in this manner?

推荐答案

只要你在工作的的水平,将有结果绝对没有区别,你获得无论你机器是小端还是大端。即只要你使用语言级的运算符(如 | &LT;&LT; 在你的例子),你将得到完全从任何平台上的上述前pression相同的算术结果。机器的字节顺序是不可检测,并在该级不可见的。

As long as you are working at the value level, there will be absolutely no difference in the results you obtain regardless of whether your machine is little-endian or big-endian. I.e. as long as you are using language-level operators (like | and << in your example), you will get exactly the same arithmetical result from the above expression on any platform. The endianness of the machine is not detectable and not visible at this level.

唯一的情况下,当你需要关心字节顺序是当你与在的对象重新presentation 的水平,也就是说,在情况下,当它的原始内存重新$ p的检查工作数据$ psentation是重要的。你上面关于 AB BC CD DE 而不是 DE CD BC AB 说是专门对原始内存布局的数据。这就是像 ntonl 功能做的:他们将一个内存布局到另一个存储布局。到目前为止,你给没有迹象表明实际原始内存布局以任何方式对你很重要。是吗?

The only situations when you need to care about endianness is when the data you are working with is examined at the object representation level, i.e. in situations when its raw memory representation is important. What you said above about "AB BC CD DE instead of DE CD BC AB" is specifically about the raw memory layout of the data. That's what functions like ntonl do: they convert one memory layout to another memory layout. So far you gave no indication that the actual raw memory layout is in any way important to you. Is it?

同样,如果你只关心的的上述前pression,它是充分的,完全的字节顺序无关。基本上,你不应该去关心字节序当你写不尝试访问并检查原始内存内容的C程序都没有。

Again, if you only care about the value of the above expression, it is fully and totally endianness-independent. Basically, you are not supposed to care about endianness at all when you write C programs that don't attempt to access and examine the raw memory contents.

这篇关于简单的位操作的小端整数,以big-endian机器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆