整数如何存储在内存中?
当我阅读一篇有关大/小端的文章时,我很困惑。
代码如下:
#include <iostream>
using namespace std;
int i = 12345678;
int main()
{
char *p = (char*)&i; //line-1
if(*p == 78) //line-2
cout << "little endian" << endl;
if(*p == 12)
cout << "big endian" << endl;
}
问题:
在第 1 行中,我可以使用
static_cast
进行转换吗?(&i) 第2行,根据代码,如果是little-endian,则
78
存储在最低字节,否则12
存储在最低字节。但我认为,i = 12345678;
将以二进制形式存储在内存中。如果是little-endian,那么
i
的二进制文件的最后一个字节将存储在最低字节中,但我不明白的是它如何保证i的最后一个字节i
是78
?就像,如果
i = 123;
,那么i
的二进制是01111011
,是否可以保证在little-endian中,23
存储在最低字节?
I'm confused when I was reading an article about Big/Little Endian.
Code goes below:
#include <iostream>
using namespace std;
int i = 12345678;
int main()
{
char *p = (char*)&i; //line-1
if(*p == 78) //line-2
cout << "little endian" << endl;
if(*p == 12)
cout << "big endian" << endl;
}
Question:
In line-1, can I do the conversion using
static_cast<char*>(&i)
?In line-2, according to the code, if it's little-endian, then
78
is stored in the lowest byte, else12
is stored in the lowest byte. But what I think is that,i = 12345678;
will be stored in memory in binary.If it's little-endian, then the last byte of
i
's binary will be stored in the lowest byte, but what I don't understand is how can it guarantee that the last byte ofi
is78
?Just like, if
i = 123;
, theni
's binary is01111011
, can it guarantee that in little-endian,23
is stored in the lowest byte?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我更喜欢
reinterpret_cast
。小端和大端是指字节(即 8 位数量)在内存中的存储方式,而不是两位十进制数量。如果
i
的值为0x12345678
,那么您可以检查0x78
和0x12
来确定字节顺序,因为两个十六进制数字对应于一个字节(在我编程的所有硬件上)。I'd prefer a
reinterpret_cast
.Little-endian and big-endian refer to the way bytes, i.e. 8-bit quantities, are stored in memory, not two-decimal quantities. If
i
had the value0x12345678
, then you could check for0x78
and0x12
to determine endianness, since two hex digits correspond to a single byte (on all the hardware that I've programmed for).这里涉及两个不同的概念:
现在,12345678 是一个十进制数,其二进制(base2)表示为 101111000110000101001110。检查起来并不容易,主要是因为 base2 表示并不容易不能精确地分组为一位小数。 (没有整数 x,因此 2x 给出 10)。
十六进制数字更容易拟合:24=16 和 28=162=256。
因此,十六进制数 0x12345678 构成字节 0x12-0x34-0x56-0x78。
现在很容易检查第一个是 0x12 还是 0x78。
(注:12345678的十六进制表示为0x00BC614E,其中0xBC为188,0x61为97,0x4E为78)
There are two different involved concept here:
Now, 12345678 is a decimal number whose binary (base2) representation is 101111000110000101001110. Not that easy to be checked, mainly because base2 representation doesn't group exactly into one decimal digit. (there is no integer x so that 2x gives 10).
Hexadecimal number are easyer to fit: 24=16 and 28=162=256.
So the hexadecimal number 0x12345678 forms the bytes 0x12-0x34-0x56-0x78.
Now it's easy to check if the first is 0x12 or 0x78.
(note: the hexadecimal representation of 12345678 is 0x00BC614E, where 0xBC is 188, 0x61 is 97 and 0x4E is 78)
static_cast
是老式 C 风格转换的一种新风格替代品,但在这里并不合适;reinterpret_cast
适用于完全更改数据类型的情况。这段代码根本不起作用——字节不能容纳偶数个十进制数字!十进制数的数字与内存中存储的字节并不一一匹配。例如,十进制 500 可以存储在两个字节中,即 0x01F4。 “01”代表 256,“F4”代表另外 244,总共 500 个。您不能说“500”中的“5”位于这两个字节中的任何一个中 - 没有直接对应关系。
static_cast
is one new-style alternative to old-fashioned C-style casts, but it's not appropriate here;reinterpret_cast
is for when you're completely changing the data type.This code simply won't work -- bytes don't hold an even number of decimal digits! The digits of a decimal number don't match up one-to-one with the bytes stored in memory. Decimal 500, for example, could be stored in two bytes as 0x01F4. The "01" stands for 256, and the "F4" is another 244, for a total of 500. You can't say that the "5" from "500" is in either of those two bytes -- there's no direct correspondence.
应该是
你不能使用static_cast。仅reinterpret_cast。
It should be
You cannot use static_cast. Only reinterpret_cast.