码迷,mamicode.com
首页 > 其他好文 > 详细

cs107_sf_note_01

时间:2017-02-21 15:57:34      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:size   python   class   补码   and   数学   证明   告诉   ret   

  1. binary digit-> just 0 or 1 计算机基本单位;
  2. 太小,只能表示是否,8 bit-> 1byte, 即|0/1|0/1|0/1|0/1|0/1|0/1|0/1|0/1|, 即2^8=256种可能;
  3. primitive type, bool/char/short/int/long/float/double;// type 决定了内存大小和相应操作,size of menory and rule of evaluation? 对于C, 在编译时候你就得告诉她要用多大,以及怎么运算,这个是静态?动态的怎么理解,说是在运行时候再解释,还没到Java,不清楚,Python怎么连type 都没有了.....暂时这么理解
  4. char, 1 byte,字符型, 每个数字对应字符,‘A‘->65->2^6+2^0->01000001;
  5. short, 2 bytes, 有正负,负数如何表示,在最高位0正1负,但是,0000 0001(+1)+1000 0001(-1)=1000 0010(-2) 明显不对, 所以负数在二进制中为了满足或者说表达出我们日常数学规则,是这么制定的,比如1111 1111再加1得 1 0000 0000(溢出)但是后8位为0,就是说为了求一个正数的相对应负数,只要这个正数加一个数得1111 1111 再加1就得到0,所以比如 0001 1101 加1110 0010(反码)得1111 1111 那么0001 1101 加 1110 0011(反码+1即补码)就为0,那么1110 0011就是0001 1101的相对应的负数,其中反码这个运算在设备层次上还容易达到。
  6. 下面用代码来拆除和验证short 中字节怎么布置,为方便,使用无符号的
    #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 上,第二字节有点像堆在上面,这里面好像有术语低地址高地址什么的,现在就不深入,用实验明白就行。

  7. 赋值语句的位模式拷贝:
    #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

  8. float: 32bit, 0/1(符号位)+8*0/1(无符号位)+2^(-1)+2^(-2)+...+2^(-9) //还有点不明白

cs107_sf_note_01

标签:size   python   class   补码   and   数学   证明   告诉   ret   

原文地址:http://www.cnblogs.com/lxjlucoding/p/6424094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!