Conversion to Binary and IEEE-754 Binary
06-27-2020, 01:35 PM
Post: #1
 Eddie W. Shore Senior Member Posts: 1,539 Joined: Dec 2013
Conversion to Binary and IEEE-754 Binary
Converting to Binary

The program BASE2 can convert any real number in Base 10 to Base 2. This extends on the built in base conversions, which works on integers only. BASE2 returns a string with precision up to 23 bits. Negative numbers will have the negative sign instead of a 2's compliment.

HP Prime Program BASE2

Code:
EXPORT BASE2(s) BEGIN // 2020-05-18 EWS // Decimal to Binary // for real x LOCAL str1,str2,str3; LOCAL d,u,w,k,x; x:=ABS(s); // set Binary mode, integer Base:=0; str1:=STRING(R→B(IP(x))); d:=DIM(str1); str1:=MID(str1,2,d-2); // fraction w:=FP(x); str2:="."; FOR k FROM 1 TO 25-d DO u:=IP(2*w); w:=FP(2*w); str2:=str2+STRING(u); END; // answer str3:=str1+str2; IF SIGN(s)==−1 THEN str3:="−"+str3; END; RETURN str3; END;

Examples:

BASE2(18.675) returns "10010.101011001100110011"

BASE2(-4.3825) returns "-100.01100001111010111000"

BASE2(π) returns "11.001001000011111101101"

Converting to IEEE-754 Format

The program IEEE754 converts any real number in decimal base to binary in IEEE-754 floating point format. A binary number in this format consists of a string of 32 bits:

Bit 1 is the sign bit: 1 for negative numbers, 0 for positive numbers

The next eight bits is the exponent bit: The exponent bit has a bias of 127. When converting to binary, 127 must be added to the exponent. (Example: 17 * 2^3. The exponent bit is 127 + 3 = 130). When converting to decimal, subtract 127 from the exponent word.

The last 23 bits consist of the mantissa.

The function returns a string with spaces between the sign, exponent, and mantissa sections for readability.

HP Prime Program IEEE754

Code:
EXPORT IEEE754(s) BEGIN // 2020-05-19 EWS // Decimal to IEEE 754 format // for real x LOCAL str1,str2,str3,str4,str5; LOCAL d,u,w,k,x,p; // determine sign bit x:=ABS(s); IF SIGN(s)==−1 THEN str1:="1"; ELSE str1:="0"; END; // set exponent // account for 127 bias // set Binary mode Base:=0; p:=127+IP(LOG(x)/LOG(2)); str2:=STRING(R→B(IP(p))); d:=DIM(str2)-2; str2:=MID(str2,2,d); // fill 0s for p<127 IF d<8 THEN FOR k FROM 1 TO 8-d DO str2:="0"+str2; END; END; // convert integer of mantissa str3:=STRING(R→B(IP(x))); d:=DIM(str3)-2; // account for # and b str3:=MID(str3,2,d); // convert fraction of matissa w:=FP(x); str4:=""; FOR k FROM 1 TO 23-d DO u:=IP(2*w); w:=FP(2*w); str4:=str4+STRING(u); END; // answer  // add spaces for clarity str5:=str1+" "+ str2+" "+ str3+str4; // RETURN {str1,str2,str3,str4,str5}; RETURN str5; END;

Example:

IEEE754(2100) returns "0 10001010 100000110100000000000000"

2100 -> 100000110100_2 -> 1.00000110100_2 * (2_10) ^ (11_10)
"moving the decimal point left 11 spaces"
Exponent bit: 127 + 11 = 138 (138_10 -> 10001010_2)

IEEE754(28.725) returns "0 10000011 11100101110011001100110"

IEEE754(-19.5) returns "1 10000011 10011100000000000000000"

Source:

Sivaraman, Abishalini. "Decimal to IEEE 754 Floating Pont Representation" YouTube Video Published December 4, 2016 https://www.youtube.com/watch?v=8afbTaA-...4_mdWQE84q

Petryk, Steve. "HOW TO: Convert Decimal to IEEE-754 Single-Precision Binary" YouTube Video. Published October 25, 2015. https://www.youtube.com/watch?v=tx-M_rqhuUA