标签:size python class 补码 and 数学 证明 告诉 ret
#include <stdio.h> int main() { printf("size of short type in c is %d\n",sizeof(short)); unsigned short a=258; printf("a is %d\n",a); printf("the first of two-bytes is %d\n",*(char*)&a); printf("the second of two-bytes is %d\n",*((char*)&a+1)); return 0; }
结果会证明,第一个字节是0000 0010,第二个字节是0000 0001,所以不是我们平时写的那样0000 0001 0000 0010,指针取得首地址在0000 0010 上,第二字节有点像堆在上面,这里面好像有术语低地址高地址什么的,现在就不深入,用实验明白就行。
#include <stdio.h> int main() { printf("size of char type in c is %d\n",sizeof(char)); printf("size of short type in c is %d\n",sizeof(short)); char a=‘A‘; printf("%d\n",a); short s=a; printf("%d\n",s); printf("%d\n",*(char *)&s); printf("%d\n",*((char *)&s+1)); return 0; }
上面代码说明了小内存想大内存赋值时候,在大内存低地址进行了 bit pattern copy
#include <stdio.h> int main() { printf("size of char type in c is %d\n",sizeof(char)); printf("size of short type in c is %d\n",sizeof(short)); short s=67; printf("%d\n",s); char a=s; putchar(a); putchar(‘\n‘); printf("%d\n",*(char *)&s); printf("%d\n",*((char *)&s+1)); return 0; }
上面代码说明大内存向小内存赋值时候,大内存的低地址跟小内存地址进行了位模式拷贝
#include <stdio.h> int main() { printf("size of short type in c is %d\n",sizeof(short)); printf("size of int type in c is %d\n",sizeof(int)); short s=-1; printf("%d\n",s); int i=s; printf("i is %d \n",i); printf("%d\n",*(short *)&s); printf("%d\n",*((char *)&s+2)); printf("%d\n",*((char *)&s+3)); return 0; }
上面代码说明了字符型小内存向大内存赋值时候,会根据最高位的0/1,向堆出来的位数全部填上0/1
标签:size python class 补码 and 数学 证明 告诉 ret
原文地址:http://www.cnblogs.com/lxjlucoding/p/6424094.html