[英]C int memory storage. Least signinficant vs most significant bits?
I'd expect the following combination of two uint8_t
(0x00 and 0x01) into one uint16_t
to give me a value of 0x0001, when I combine them consecutively in memory.当我在 memory 中连续组合它们时,我希望以下两个
uint8_t
(0x00 和 0x01)组合成一个uint16_t
给我一个 0x0001 的值。 Instead I obtain 0x0100 = 256, which I'm surprised of.相反,我得到了 0x0100 = 256,这让我很惊讶。
#include <stdio.h>
#include <stdint.h>
int main(void){
uint8_t u1 = 0x00, u2 = 0x01;
uint8_t ut[2] = {u1, u2};
uint16_t *mem16 = (uint16_t*) ut;
printf("mem16 = %d\n", *mem16);
return 0;
}
Could anyone explain me what I've missed in my current understanding of C memory?谁能解释一下我目前对 C memory 的理解中遗漏了什么? Thank you: :-)
谢谢: :-)
It is called endianess
.它被称为
endianess
。
Most system nowadays use little endian.现在大多数系统都使用小端。 In this system first is stored the least significant byte.
在这个系统中,首先存储最低有效字节。 So the 0x0100 is stored (assuming 2 bytes representation) as
{0x00, 0x01}
exactly as in your case所以 0x0100 存储(假设 2 个字节表示)为
{0x00, 0x01}
与您的情况完全相同
ut[0] is inserted on LSB of mem16, and ut[1] on MSB. ut[0] 插入 mem16 的 LSB, ut[1] 插入 MSB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.