HP Forums
(50g) B->I and I->B - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (50g) B->I and I->B (/thread-15203.html)



(50g) B->I and I->B - Joe Horn - 06-17-2020 03:16 AM

Here are two little User RPL programs that convert exact integers into binary numbers and vice versa, without the roundoff errors caused by the R->B and B->R functions. These conversions are trivial to do in System RPL, but that's no fun. Faster and/or shorter alternatives to the following would be delightful to see. The default wordsize of 64 may be assumed.

I->B (Integer to Binary)
Input: Any exact integer (no decimal point) between 0 and 2^64-1
Output: The exact equivalent "user binary" (AKA binary number).
Example:
2 41 ^ I->B --> #20000000000h (exact, unlike R->B)

Code:
\<< "#" SWAP + "d" + OBJ\-> \>>
BYTES: 32 #5237h

B->I (Binary to Integer)
Input: Any "user binary" (AKA binary number).
Output: The exact equivalent integer.
Example:
#123456789012345h B->I --> 81985529205302085 (exact, unlike B->R, R->I)

Code:
\<< PUSH DEC \->STR 3 OVER SIZE 1 - SUB OBJ\-> POP \>>
BYTES: 43.5 #2F83h