In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how float is stored in memory in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Recently, when talking about the basis of Java, talking about data type conversion, it is mentioned that when integer type long-byte type data is put into short-byte type data, the data will be truncated because there are not enough bytes, so programmers are required to manually force type conversion, such as putting int-type data into byte types.
Then the student asked why float has only 4 bytes, while long has 8 bytes. There is no need to cast when putting an long data into a float, while putting float into a long requires a cast.
As for why long is put into float without forced conversion, you need to understand the mechanism by which float stores data. Data stored in float is counted in a scientific way, and all the integers that can be stored are larger than the long type, so there is no problem for long data to be put into float, but there may be a lot of error.
Specifically, when you store a number in float, you do this:
Float has 4 bytes, or 32 bit
32 31 30-24 23-1
Symbol bit index number
1. Convert a decimal to a 2 decimal.
two。 After moving the decimal point (left or right) to the first number 1, you get a similar 1.01001. Such a binary decimal m.
3. Fill in the sign bit of the 32nd bit (the highest bit) according to the positive or negative of the decimal, with a positive number and a negative number.
4. Determine the value of bit 31 according to the direction of movement, 1 if you move to the left, move to the right or do not move.
5. If you are moving to the left, convert nmur1 to binary, and fill in zero to 7 bits on the left, filling in the exponential bits of 30-24 bits. If it is not moving or moving to the right, convert n to binary, and fill in zero to 7 bits on the left, and fill each bit back to 30-24 bits.
6. After removing the integer place and decimal point of the number m, truncate the length of 23 digits and fill it to 23-1 digits.
For example, the number 12.1
Converted to binary system: 1100.0001100110011001100110011001100110011001100110011
Move the number to the left by 3 digits: 1.10000011001100110011001100110011001100110011001100110011
Because the number 12.1 is positive, the 32 of float is.
Because it moves to the left, the 31st place is: 1
Because it moved 3 places to the left, 3-1-2, so the 30th-24th place is: 0000010
Finally, truncate 23 digits after the decimal point, so the 23-1 digit is: 1000001 10011001 10011001
The final 12.1 is represented as binary and saved in memory as: 01 0000010 1000001 10011001 10011001
This is the end of the content of "how float is stored in memory in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.