How does FLOAT store in memory?

Float is a floating-point type, 32-bit machine occupies 4 bytes, a total of 32bit, subscript 0~31.

31 bit: sign bit, positive number is 0, negative number is 1 .

30 bits: direction bits. The decimal point is shifted by 1 to the left and 0 to the right.

23~29: A total of 7 digits, index digits. = Index -1.

0~22: 23 digits in total, mantissa.

Conversion method:

1 The integer part is converted to binary. The integer keeps dividing by 2 until the quotient is 0. Take out the remainder each time in reverse order.

2 The decimal part is converted to binary. Multiply the decimal part by 2 until the decimal part of the result is 0. Take the quotient every time in a positive sequence.

3 The first and second steps are to get the data splicing. The result obtained in the first step is to the left of the decimal point, and the result obtained in the second step is to the right of the decimal point. Switch to scientific notation.

4 splicing. Fill in according to the bit instructions above. The mantissa is not enough, add 0 on the right.

For example: write out 8.25 12.5 0.25 is how to save it in the memory.

8.25 storage calculation method:

  1. The integer part 8 is converted to binary:

    8/2=4 more than 0

    4/2=2 more than 0

    2 /2=1 Remaining 0

    1/2=0 Remaining 1 Quotient 0 Then stop. Get the reverse order 1000    

2. The decimal part 0.25 is converted to binary:

    0.25 *2=0.5 integer part 0

    0.5*2=1.0 The integer part once is the decimal part 0 and stop. Get the positive sequence01

  3, use binary:

    1000.01 = 1.00001 * 2 to the third power. The decimal point is shifted to the left by 3 places.

  4、

    31 :0

    30: 1

    Exponent bit: 3-1=2=0000010

     mantissa: 0000 1000 0000 0000 0000 000

   final: 0100 0001 0000 0100 0000 0000 0000 0000=0x41040000

   It can be seen from the above that if it is -8.25, only the highest bit needs to be changed to 1. Final: 1100 0001 0000 0100 0000 0000 0000 0000 = 0xC1040000

  

12.5 conversion process:

1 integer part:

    12/2 = 6 more than 0

    6/2=3 more than 0

3/2=1 Remaining 1

    1/2=0 Remaining 1

Get the remainder in reverse order 1100

2 Decimal part:

0.5*2=1.0 The integer is 1

   gets 1

3: Get binary: 1100.1=1.1001*2 to the third power

4: 0 1 0000010 1001 0000000000000000000 = 0x41480000

0.25 conversion process

1 The integer part is converted to 0

2 The decimal part 0.25

    0.25*2=0.5 integer part 0

0.5*2=1.0 integer part 1

p>

3 get 0.01 = 1.0*2 to the -2 power

4 get: 0 0 1111101 0000000000000000000000=0x3E800000 exponent part -2-1=-3=FD=11111101 Take 7 bits

Leave a Comment

Your email address will not be published.