位域,简单的说就是在结构体中定义了变量的位数
比如下面就是一字节八位
#include<iostream>
#include<memory.h>
using namespace std;
struct A {
int a : 5;
int b : 3;
};
int main(void) {
char str[100] = "01234324324afsadfsdlfjlsdjfl";
static struct A d;
memcpy(&d, str, sizeof(A));
cout << d.a<<endl;
cout << d.b << endl;
}
输出是-16 1
"0"的ASCII码是00110000然后拷贝到d中是a:10000 b:001
没什么难的,可是我犯了一个低级错误:错误的理解了数字的高低位和内存的高低位
唉,惭愧啊
数字的低位就是内存的低位
所以拷贝是从右往左的
很基础的问题,但是我一直没有解决。
惭愧惭愧
附一张图:
所以一定不要轻视基础!