简体   繁体   English

C int memory 存储。 最低有效位与最高有效位?

[英]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.

相关问题 C:获取最高有效位和最低有效位 - C: Getting the most significant and least significant bits C-如何获取Char中的4个最低有效位 - C - How to get the 4 least significant bits in a Char 如何仅反转整数的最高有效位,而保持另一半(最低有效位)不变? - How to reverse bits in only the most significant half of an integer and leave the other half (least significant bits) untouched? 使用 C 中的运算符从无符号整数中获取 6 个最高有效位的最佳方法? - Best way to grab 6 most significant bits from an unsigned int using operators in C? 函数指针中的最低有效位 - Least significant bits in function pointer 在c中检索signed long long的最高有效32位 - retrieving most significant 32-bits of a signed long long in c 按位切换最高有效字节和最低有效字节 - Bitwise switch most significant and least significant bytes 在 C/C++ 中检查最低有效位 (LSB) 和最高有效位 (MSB) 的值 - Check value of least significant bit (LSB) and most significant bit (MSB) in C/C++ 如何用C中的最高有效位(MSB)替换最低有效位(LSB) - How to replace the the least significant bit (LSB) with most significant bit (MSB) in C 创建一个设置了 N 个最低有效位的掩码 - Creating a mask with N least significant bits set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM