整数如何存储在内存中?

发布于 2024-12-05 14:24:59 字数 831 浏览 2 评论 0原文

当我阅读一篇有关大/小端的文章时,我很困惑。

代码如下:

#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. 在第 1 行中,我可以使用 static_cast(&i) 进行转换吗?

  2. 第2行,根据代码,如果是little-endian,则78存储在最低字节,否则12存储在最低字节。但我认为,i = 12345678;将以二进制形式存储在内存中。

    如果是little-endian,那么i的二进制文件的最后一个字节将存储在最低字节中,但我不明白的是它如何保证i的最后一个字节i78

    就像,如果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:

  1. In line-1, can I do the conversion using static_cast<char*>(&i)?

  2. In line-2, according to the code, if it's little-endian, then 78 is stored in the lowest byte, else 12 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 of i is 78?

    Just like, if i = 123;, then i's binary is 01111011, can it guarantee that in little-endian, 23 is stored in the lowest byte?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昵称有卵用 2024-12-12 14:24:59
  1. 我更喜欢reinterpret_cast

  2. 小端和大端是指字节(即 8 位数量)在内存中的存储方式,而不是两位十进制数量。如果 i 的值为 0x12345678,那么您可以检查 0x780x12 来确定字节顺序,因为两个十六进制数字对应于一个字节(在我编程的所有硬件上)。

  1. I'd prefer a reinterpret_cast.

  2. 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 value 0x12345678, then you could check for 0x78 and 0x12 to determine endianness, since two hex digits correspond to a single byte (on all the hardware that I've programmed for).

坐在坟头思考人生 2024-12-12 14:24:59

这里涉及两个不同的概念:

  1. 数字以二进制格式存储。 8 位表示一个字节,整数可以使用 1、2、4 甚至 8 或 1024 个字节,具体取决于它们运行的​​平台。
  2. Endiannes 是字节在内存中的顺序(低位在前 - LE,或高位在前 - BE)

现在,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:

  1. Numbers are stored in binary format. 8bits represent a byte, integers can use 1,2,4 or even 8 or 1024 bytes depending on the platform they run on.
  2. Endiannes is the order bytes have in memory (less significant first - LE, or most significant first - BE)

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)

感性 2024-12-12 14:24:59
  1. static_cast 是老式 C 风格转换的一种新风格替代品,但在这里并不合适; reinterpret_cast 适用于完全更改数据类型的情况。

  2. 这段代码根本不起作用——字节不能容纳偶数个十进制数字!十进制数的数字与内存中存储的字节并不一一匹配。例如,十进制 500 可以存储在两个字节中,即 0x01F4。 “01”代表 256,“F4”代表另外 244,总共 500 个。您不能说“500”中的“5”位于这两个字节中的任何一个中 - 没有直接对应关系。

  1. 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.

  2. 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.

温柔一刀 2024-12-12 14:24:59

应该是

unsigned char* p = (unsigned char*)&i;

你不能使用static_cast。仅reinterpret_cast。

It should be

unsigned char* p = (unsigned char*)&i;

You cannot use static_cast. Only reinterpret_cast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文