Hexadecimal floating-point number format

Source: Internet
Author: User

About 16 binary floating point numbers
For floating-point numbers with size 32-bit (32-bit is single precision, 64-bit floats are double precision, 80-bit is extended precision floating point numbers),
1, its 31st bit is a sign bit, 0 is a positive number, the inverse is a plural, its reading value is expressed in S;
2, the 30th to 23rd bit is a power number, its reading value is expressed by E;
3. The 22nd to No. 0 bit, as a factor, is considered as a binary decimal fraction, assuming that the decimal value of the decimal is x;

By default, the value of the floating-point number is expressed in decimal:
= ( -1) ^s * (1 + x) * 2^ (e-127)

For 49e48e68,
1, its 31st bit is 0, that is, s = 0
2, the 30th to 23rd bit is 100 1001 1, read in decimal is 147, that is, E = 147.
3. The 22nd to No. 0 bit is 110 0100 1000 1110 0110 1000, which is the binary decimal fraction 0.110 0100 1000 1110 0110 1000, which is in decimal form 0.78559589385986328125, or x = 0.7 8559589385986328125.

In this way, the decimal representation of the floating-point number
= ( -1) ^s * (1 + x) * 2^ (e-127)
= (-1) ^0 * (1+ 0.78559589385986328125) * 2^ (147-127)
= 1872333

The above content is the IEEE standard, now has the question, suppose I have two devices in the communication, one device sends the data to another device, this data is the floating point number, then how can I send this data? How do you let the recipient know that a floating point is being sent? and send the data to be as short as possible, it is best to fix the length, perhaps someone will do: A send data 3.7586 to b,a first to multiply this floating point number 10000 into an integer 37586, and then send this integer to B, so the disadvantage is: 1, b to know in advance how many times a will enlarge the number. 2, a need to do a floating-point multiplication, and b to do a floating-point division operation, which is a burden on the microcontroller. 3, so use will be more occupation of sending data. Because no one in advance knows how big the floating point is.

Think for a while, think that using the IEEE floating-point rules to send is the most reliable, because any floating-point number is represented as 4 bytes, which is good for both sending and receiving, as long as both sides know that the floating point to be sent on it. and the IEEE format floating-point conversion is performed inside the machine, we no longer need any conversion, no increase in the amount of computation, no increase in the amount of code.
Following this principle, the test code is written as follows:
Sender A:
float Fsend; A floating point data that needs to be sent
Char chsend[4]; Sends the buffer, after the converted floating point data, becomes a character type array.
The following is a conversion
CHSEND[0] = * ((char *) (&fsend));
CHSEND[1] = * ((char *) (&fsend) + 1);
CHSEND[2] = * ((char *) (&fsend) + 2);
CHSEND[3] = * ((char *) (&fsend) + 3);

At this point a can send this number to B, and B receives a 4-byte floating-point, but it needs to be converted as follows:

float freceive; Floating-point number received
Char chreceive[4];//receive buffering. b Save the 4 bytes received here.
The following is a conversion
* ((char *) (&freceive)) = Chreceive[0];
* ((char *) (&freceive) + 1) = chreceive[1];
* ((char *) (&freceive) + 2) = chreceive[2];
* ((char *) (&freceive) + 3) = chreceive[3];

Well, at this point B has got a floating-point number freceive;

Transferred from: http://blog.csdn.net/educast/article/details/8522818

Hexadecimal floating-point number format

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.