https://blog.csdn.net/yansmile1/article/details/70145416/

#include <bits/stdc++.h>
using namespace std;
int main() {
    double x = 2.5;
    char* p = (char*)(&x);
    for (int i = sizeof(double) - 1; i >= 0; --i) {
        for (int j = 7; j >= 0; --j) {
            printf("%c", '0' + ((p[i] >> j) & 1));
        }
    }
    return 0;
}

符号位 阶码 尾数 长度
float 1 8 23 32
double 1 11 52 64

所以float和 double类型分别表示的2.5如下(二进制):\
符号位 指数 尾数
0 1000 0000 010 0000 0000 0000 0000 0000
0 100 0000 0000 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

#include <bits/stdc++.h>
using namespace std;
int main() {
    float x = 2.5;
    char* p = (char*)(&x);
    for (int i = sizeof(x) - 1; i >= 0; --i) {
        for (int j = 7; j >= 0; --j) {
            printf("%c", '0' + ((p[i] >> j) & 1));
        }
    }
    return 0;
}

https://blog.csdn.net/salmonwilliam/article/details/114369825

发表评论