The storage format and data acquisition of floatdouble in the memory in Java

Source: Internet
Author: User

The byte format of floating point data is as follows:

Address + 0 + 1 + 2 + 3
Content: seee eeee emmm mmmm

Here
S indicates the symbol bit, 1 is negative, 0 is positive
The power of e offset 127. The binary level code is (eeeeeeeeee)-127.
The M 24-bit ending number is stored in 23 bits, only 23 bits are stored, and the highest bit is fixed as 1. This method is implemented with a minimum number of digits.
A high number of valid digits improves accuracy.

Zero is a specific value, and the power is 0 or 0.

The floating point-12.5 is stored in the storage area as a hexadecimal number 0xc1480000. The value is as follows:
Address + 0 + 1 + 2 + 3
Content 0xc1 0x48 0x00 0x00

The conversion between a floating point and a hexadecimal value is quite simple. The following example shows how to convert the value-12.5
Change.
The floating point storage value is not a direct format. to convert it to a floating point number, the bit must be saved in the above floating point storage format table.
As shown in the following table, for example:

Address + 0 + 1 + 2 + 3
Format: seee eeee emmm mmmm
Binary 11000001 01001000 00000000 00000000
Hexadecimal C1 48 00 00

The following information can be obtained from this example:
1 indicates a negative number.
The power is binary 10000010 or decimal 130,130 minus 127 is 3, which is the actual power.
The ending number is the binary number following 10010000000000000000000

There is an omitted decimal point and 1 on the left side of the ending number. This 1 is often omitted in the storage of floating point numbers.
Start with the ending number and obtain the ending value as follows:
1.10010000000000000000000

Next, adjust the ending number according to the index. A negative index moves the decimal point to the left. A positive index moves the decimal point to the right. Because
The index is 3, and the ending number is adjusted as follows:
1100.10000000000000000000

The result is a binary floating point number. The binary number on the left of the decimal point represents the power of 2, for example, 1100.
(1*2 ^ 3) + (1*2 ^ 2) + (0*2 ^ 1) + (0*2 ^ 0) = 12.
The right of the decimal point also represents the power of 2, but the power is negative. Example:. 100... indicates (1*2 ^ (-1) +
(0*2 ^ (-2) + (0*2 ^ (-2) .... = 0.5.
The sum of these values is 12.5. Because the set symbol bit indicates that the number is negative, the hexadecimal value 0xc1480000 indicates-
12.5. Storage of Multi-byte data types in memory

INT and short are 4 and 2 bytes respectively. Their memory storage methods are described as follows.

Int DATA = 0xf4f3f2f1;
The low part is stored in the memory unit with a small address, and the high part is stored in the memory unit with a high address.
As follows:
Address: 0x8000 0x8001 0x8002 0x8003
Data: F1 F2 F3 F4 processes floating point numbers according to IEEE standards set in 1985
The single-precision floating point uses four bytes, including one-bit symbol (s) (integer is 0, negative number is 1), eight-bit exponential E, and 23-bit valid F
Float uses scientific notation. For example, decimal 12345 can be expressed as 1.2345*10 ^ 4 (representing the four power of 10)
In binary format: 1.1000000111001*2 ^ 13
So in the computer, the floating point number indicates that the decimal value of 12345 should be like this. The S bit is 0. Because it is a positive number, the index bit is 13 + 127 = 140 (127 is the single-precision floating point offset value, to indicate the number of digits with only decimals), the valid bit is 1000000111001.
When calculating with (-1) ^ s * 1.f * 2 ^ (e-127), the result is 1*1.1000000111001*2 ^ (140-127 = 13 ), same as what we just said
For example, if decimal 0.125 is converted to binary decimal 0.001, it can be expressed as 1*1.0*2 ^ (124-127 =-3)
Double, double-precision floating-point numbers include 1-bit sign-bit, 11-bit index-bit, and 52-bit valid number. Thanks, it is similar to what I found :)
Know the formula
N = (-1) ^ s * m * 2 ^ e
E = | E |-bias
Bias = 2 ^ (k-1)-1 (k is the number of digits of E)
M = | 1. M |

After we know that 12345 is represented in the 10-in-memory format
0x4640e400 = 0 (100 0110 0) <100 0000 1110 0100 0000>
The number in the brackets is | E | = 140 so E = 140-127 = 13
The number in the angle brackets is M = | 1. M | = | 1.100000011100100 | = 1.506958008
OK,
Enter the formula n = (-1) ^ 0*1.506958008*2 ^ 13 = 12345 and attach the data conversion tool class you have written below:

Package com. zhcw. tools;
Import java. Text. decimalformat;

Public class datautil {
Private Final Static char [] hex = "0123456789 abcdef". tochararray ();

/**
* Convert int type data to a binary string. If the number of digits of the int type is less than the number of digits of the int type, add "0" to make up the number of digits.
*
* @ Param num
* @ Return
*/
@ Suppresswarnings ("UNUSED ")
Private Static string tofullbinarystring (INT num ){
Char [] CHS = new char [integer. Size];
For (INT I = 0; I <integer. size; I ++ ){
CHS [integer. Size-1-I] = (char) (Num> I) & 1) + '0 ');
}
Return new string (CHS );
}

/** Datautil
* Convert int type data to a hexadecimal string. If the number of digits of the int type is less than the number of digits of the int type, add "0" to the front to make up the number of digits.
*
* @ Param num
* @ Return
*/
@ Suppresswarnings ("UNUSED ")
Private Static string tofullhexstring (INT num ){
Char [] CHS = new char [integer. Size/4];
For (INT I = 0; I <CHS. length; I ++ ){
CHS [chs. Length-1-I] = hex [(Num> (I * 4) & 0xf];
}
Return new string (CHS );
}

/**
* Convert long data to a binary string. If the number of digits of the long type is not enough, add "0" to the front to make up the number of digits.
*
* @ Param num
* @ Return
*/
@ Suppresswarnings ("UNUSED ")
Private Static string tofullbinarystring (long num ){
Char [] CHS = new char [long. Size];
For (INT I = 0; I <long. size; I ++ ){
CHS [long. Size-1-I] = (char) (Num> I) & 1) + '0 ');
}
Return new string (CHS );
}

/**
* Convert long data to a hexadecimal string. If the number of digits of the long type is not enough, add "0" to the front to make up the number of digits.
*
* @ Param num
* @ Return
*/
@ Suppresswarnings ("UNUSED ")
Private Static string tofullhexstring (long num ){
Char [] CHS = new char [long. Size/4];
For (INT I = 0; I <CHS. length; I ++ ){
CHS [chs. Length-1-I] = hex [(INT) (Num> (I * 4) & 0xf)];
}
Return new string (CHS );
}

/**
* @ Param ARGs
*/
Public static string getdoublenum (string hexstring ){
String STR = hexstring;
Int index;
Int sym;
Double result;
Byte [] stebyte2 = NULL;
String byte2string = converthextobinary3 (STR );
Index = getindex (byte2string );
Sym = getsymbol (byte2string );
System. Out. println ("sym->" + sym );
Stebyte2 = getbyte2buf (byte2string );
Result = getintnum (stebyte2, index) * sym;
System. Out. println ("result->" + result/100 );
Decimalformat df = new decimalformat ("### 0"); // a maximum of several decimal places can be saved. The minimum number is determined by 0.
String S = DF. Format (result/100 );
System. Out. println ("result->" + S );
Return S;

}

/***
* The byte format of floating point numbers is as follows: Float address + 0 + 1 + 2 + 3 content seee eeee emmm mmmm
* Mmmm floating point data is saved in the following byte format: Double N = (-1) ^ s * m * 2 ^ e = | E |-bias =
* 2 ^ (k-1)-1 (k is the number of digits of E) M = | 1. M |
*/

@ Suppresswarnings ("UNUSED ")
Private string getlongbits (long ){
// Read the 8-byte array
System. Out. println ("getlongbits ");
Byte [] B = new byte [8];
For (INT I = 7; I> = 0; I --){
B [I] = (byte) (A & 0x000000ff );
A = A> 8;
}
System. Out. println ("over ");
Return byte2hex (B); // call the following function
}

/**
* Convert the byte array to a hexadecimal string.
*
* @ Param B
* @ Return string
*/
Private Static string byte2hex (byte [] B ){

System. Out. println ("byte2hex ");
Stringbuffer sb = new stringbuffer ();
String stmp = "";
For (INT n = 0; n <B. length; n ++ ){
Stmp = (integer. tohexstring (B [N] & 0xff ));
If (stmp. Length () = 1 ){
// Zero at the end of less than two digits
SB. append ("0" + stmp );
} Else {
SB. append (stmp );
}
If (sb. Length () % 2 = 0) // ":" As delimiter
SB. append (":");
System. Out. println ("byte2hex over->" + n );
}
System. Out. println ("byte2hex over ");
Return sb. tostring (). touppercase ();
}

/**
* Convert a hexadecimal string to a binary string.
*
* @ Param hexstring
* @ Return string
*/
Private Static string converthextobinary3 (string hexstring ){
Long L = long. parselong (hexstring, 16 );
String binarystring = long. tobinarystring (L );
Int shouldbinarylen = hexstring. Length () * 4;
Stringbuffer addzero = new stringbuffer ();
Int addzeronum = shouldbinarylen-binarystring. Length ();
For (INT I = 1; I <= addzeronum; I ++ ){
Addzero. append ("0 ");
}
System. Out. println ("converthextobinary3->" + addzero. tostring ()
+ Binarystring );
Return addzero. tostring () + binarystring;

}

/**
* Obtain the index from the 1-12 bits of the binary string.
*
* @ Param Str
* @ Return int
*/
Private Static int getindex (string Str ){
String index_str = "";
Int index_int;
Int base_index = (INT) Java. Lang. Math. Pow (2, 10)-1;
System. Out. println ("base_index->" + base_index );
Index_str = Str. substring (1, 12 );
System. Out. println ("index_str->" + index_str );
Index_int = integer. valueof (index_str, 2)-base_index;
System. Out. println ("getindex->" + index_int );
Return index_int;
}

/**
* Judge the symbol bit from the first digit.
*
* @ Param Str
* @ Return int
*/
Private Static int getsymbol (string Str ){
System. Out. println ("getsymbol->" + Str. charat (0 ));
If (Str. charat (0) = 48)
Return 1;
Else
Return-1;
}

/**
* Get the M bit from the binary string and put it in the byte array to return
*
* @ Param Str
* @ Return byte []
*/
Private Static byte [] getbyte2buf (string Str ){
String STRM = "";
Byte [] stebyte2 = new byte [Str. Length () + 1];
System. Out. println ("stebyte2->" + Str. Length ());
STRM = Str. substring (12, 63 );
STRM = "1". Concat (STRM );
For (byte I = 0; I <STRM. Length (); I ++)
Stebyte2 [I] = (byte) STRM. charat (I );
System. Out. println ("STRM->" + STRM );
Return stebyte2;
}
/**
* Calculate the decimal number based on the input parameter M bit and exponential size
* @ Param byte [] bytebuf, int Index
* @ Return double
*/
Public static double getintnum (byte [] bytebuf, int index ){
Double sum = 0;
Int TMP =-1;
For (byte I = 0; I <bytebuf. Length & index> = 0; I ++, index --){
If (bytebuf [I] = 49 ){
TMP = 1;
} Else
TMP = 0;
Sum + = TMP * java. Lang. Math. Pow (2, index );
}
System. Out. println ("sum->" + sum );
Return sum;
}
}

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.