Code From Class 2026/05/06

The binary expansion of \(x=\frac{1}{10}=0.1\) has infinitely many digits \begin{eqnarray*} x&=&0.000110011001100110011\dots\\ &=&1.1001100110011001100110011\dots \cdot 2^{-4}. \end{eqnarray*}

If we use float, then the sign is \(0\), the exponent is \(-4\) and the normalized mantissa is \[1.1001100110011001100110011\dots\] The digit before the dot is not stored with normalized mantissa. The content of the memory should be \(0\) for the sign, the exponent \(-4\) is shifted by \(127\) and is \(123\). The content of the memory is

00111101110011001100110011001101

The last digit is 1 because the rounding made 0 into 1.

#include<iostream>
std::string binary(int x){
    std::string result;
    for(long i=0;i<32;++i){
        result=std::to_string(x%2)+result;
        x = x/2;
    }
    return result;
}
int main(){
    float x=0.1;
    void* aX= (void*) (&x);
    int* aW= (int*) (aX);
    std::cout<<binary(*aW)<<"\n";
    return 0;
}