1. #include <stdio.h>
    2. typedef unsigned char byte;
    3. typedef byte BIT8;
    4. typedef unsigned char * MEMCUR;
    5. int isEndianMode();
    6. void memory_layout(MEMCUR start, int len){
    7. char *binStr[16] = {
    8. "0000","0001",
    9. "0010","0011",
    10. "0100","0101",
    11. "0110","0111",
    12. "1000","1001",
    13. "1010","1011",
    14. "1100","1101",
    15. "1110","1111"
    16. };
    17. if(!len) len=sizeof(MEMCUR*);
    18. MEMCUR cur = start;
    19. int iter;
    20. int AL,AH;
    21. if(!isEndianMode()){
    22. cur = start+3;
    23. for(iter = 0; iter < len;iter++,cur--){
    24. AL = *cur & 0b00001111;
    25. AH = *cur >> 4;
    26. printf("%p %s %s %.2X\n",cur,binStr[AH], binStr[AL], *cur);
    27. }
    28. }else{
    29. for(iter = 0; iter < len;iter++,cur++){
    30. AL = *cur & 0b00001111;
    31. AH = *cur >> 4;
    32. printf("%p %s %s %.2X\n",cur,binStr[AH], binStr[AL], *cur);
    33. }
    34. }
    35. }
    36. int isEndianMode(){
    37. unsigned char *cursor; //单字节的游标指针
    38. unsigned short num = 0xff00; //储存两个字节的数
    39. cursor = (unsigned char *)&num; //进行类型转换
    40. if (*cursor < *(cursor + 1)) //如果高地址值更大
    41. return 0;
    42. else return 1;
    43. }
    44. /*
    45. value = sign * exp * frac
    46. V = (-1)^s * M * 2^E
    47. E = e - bias
    48. bias = 127(m32)
    49. bias = 1023(m64)
    50. e bias E 2^E f m value
    51. NaN not a number
    52. inf infinte
    53. */
    54. int main(){
    55. int te = 0xABCDEF12;
    56. memory_layout((MEMCUR)&te,4);
    57. float x = -1;
    58. printf("sizeof x in m64 is %d\n", sizeof(x));
    59. printf("%f\n",x);
    60. printf("%p\n",&x);
    61. memory_layout((MEMCUR)&x,4);
    62. x = 1;
    63. printf("---------\n");
    64. memory_layout((MEMCUR)&x,4);
    65. printf("%f\n",x);
    66. x = 0.5;
    67. printf("---------\n");
    68. memory_layout((MEMCUR)&x,4);
    69. printf("%f\n",x);
    70. x = 0.25;
    71. printf("---------\n");
    72. memory_layout((MEMCUR)&x,4);
    73. printf("%f\n",x);
    74. return 0;
    75. }
    76. /*
    77. 0011 1111 3F
    78. 1110 0000 E0
    79. 0001 0101 15
    80. 0001 0111 17
    81. 1011 1111 BF
    82. 1110 0000 E0
    83. 0001 0101 15
    84. 0001 0111 17
    85. */