HP Forums

Full Version: (50g) B->I and I->B
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Reference URL's