A program that converts a single-precision floating point to an integer in the storage format.

Source: Internet
Author: User

A program that converts a single-precision floating point to an integer in the storage format.
 

/// // # Include
 
  
// ----------------- Union int_char {unsigned char ch [4]; float I;}; void out_put (union int_char x) // x86 is the mode of small end-to-end, that is, the lowest data bit is stored in the second bit of the address. {Printf ("Single-precision floating point value: % f \ n", x. i, x. i); printf ("storage position from left to right, from high to low is 0x: \ n"); printf ("% X", x. ch [3]); // The highest bit printf ("% X", x. ch [2]); printf ("% X", x. ch [1]); printf ("% X \ n", x. ch [0]); // bytes} // ------------------ int main (int argc, char * argv []) {union int_char x; // x. I = 123.456; // x. I = 1.0; x. I = 3.0; out_put (x); system ("pause"); return 0 ;}
 

Single-precision floating point number

1. The length of the single-precision floating point number defined in the IEEE 754 standard is 32 bits, which can be divided into symbol bit, level code bit, and tail number bit by bit field, as follows:

California | X | ------------- | | ---------------------------------------------------------- | tail number of signed Order Codes

The sign bit 0 indicates a positive number, and the value 1 indicates a negative number.

The level code bit is 8 bits. here we need to note that the index n cannot be processed as a Level Code directly. We need to compare it with 127 (0x7f) the level code representation that can be obtained only by adding.

The length of the ending number bit field is 23 characters in the figure, but it is actually 24 digits. This bit is "invisible" and its value is fixed to 1, this is the floating point number defined by the IEEE 754 standard. Its valid number is a decimal point between 1 and 2.

You can try to write the binary Single-precision floating point format of the number 1.0, which helps you better understand the Bit Field Distribution of the single-precision floating point format.

1.0 binary Single-precision floating point format: 0 0111 1111 0000 0000 0000 0000 0000

It is worth noting that the book says that we need to add 127 to the index to get the order code to simplify the comparison of floating point numbers. However, the offset of 127 (shift code) can be used to distinguish between positive and negative values of the index. When the level code is 127, it indicates that the index is 0; when the level code is less than 127, it indicates a negative index; when the level code is greater than 127, it indicates a positive index.

 

 

2. First convert the decimal number 123.456 to the binary number: 1111011. 01110100101111001

(How do I convert 0.456 to binary? Take the 2nd round and arrange them in sequence

For example, if 0.734375 is converted to binary, the result is 101111.

0.734375x2 = 1.46875
0.46875x2 = 0.9375
0.9375x2 = 1.875
0.875x2 = 1.75
0.75x2 = 1.5
0.5 × 2 = 1.0)

 

1111011. 01110100101111001 is the power of 1. 11101101110100101111001 multiplied by the power of 6 of 2

First, this is a positive number, and the symbol bit is 0.

The order code is 6, but it must be converted to a shift code.

(How to calculate 6Code Transfer? I am not very familiar with it here. I see that everyone is directly 6 + 127 = 133, in a 2-digit System of 10000101)

(The relationship between the shift code and the complement code: the relationship between the [X] shift and the [X] fill is the opposite of the sign bit (only the symbol bit is different ))

The ending number is the decimal part of 1. 11101101110100101111001, that is

11101101110100101111001

In summary, the binary storage format of 123.456 is 01000010111101101110100101111001.


The code execution result is as follows:

Floating Point: 1.0 (0 0111 1111 0000 0000 0000 0000)

The order from high to low is: single precision floating point value: 1.000000
The storage location is from left to right, and the order from high to low is 0x:
3F 80 0 0

======================================

Floating Point: 123.456 (01000010111101101110100101111001)

The single-precision floating point value is 123.456001.
The storage location is from left to right, and the order from high to low is 0x:
42 F6 E9 79


Press any key to continue...

 

 

The following code can also be used:

 

 

Float I = 3.0; unsigned char * p = (unsigned char *) & I; printf ("single precision floating point value: % f \ n", I ); printf ("storage position from left to right, from high to low is 0x: \ n"); printf ("% X", * (p + 3 )); printf ("% X", * (p + 2); printf ("% X", * (p + 1 )); printf ("% X \ n", * p );


 

The results are the same.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.