Post Reply 
(15C) another approach to decimal <-> binary conversion
11-29-2017, 10:32 AM (This post was last modified: 11-29-2017 04:31 PM by jthole.)
Post: #1
(15C) another approach to decimal <-> binary conversion
This is my first try at creating a useful RPN program, so any comments for improvement are welcome! Smile

A well known strategy for representing a decimal number in binary notation, is to keep dividing by two, and placing a "1" if the fractional part > 0, and a "0" if the fraction is 0. At the end of the conversion, the binary result is the output read backwards. That works well for uneven numbers, but you will lose zeros for even numbers. I've written my own conversion (not using an existing program as the basis, so I surely have made beginner mistakes), which prefixes the number with a "8". That makes sure the leading zeros are preserved, and can be easily read as "B" for binary ;-)

This program takes 20 steps, and uses storage register 0 for intermediate results. Since I am still waiting for the DM15L to arrive, I tested this in the following HP 15C emulators:
- Touch 15C on Android
- The online hp15c.com emulator

Code:
001 8
002 X <> Y
003 LBL A
004 2
005 /
006 STO 0
007 FRAC
008 2
009 x
010 X <> Y
011 1 (entering '10' takes two steps)
012 0
013 x
014 +
015 RCL 0
016 INT
017 TEST 1
018 GTO A
019 X <> Y
020 RTN

To start the program, input the decimal number you want to convert (no need to press enter), and press R/S. The binary number is returned in the X register (in reverse).

Here's the same program for the HP 12C:

Code:
01 8
02 X <> Y
03 2
04 /
05 STO 0
06 FRAC
07 2
08 x
09 X <> Y
10 1
11 0
12 x
13 +
14 RCL 0
15 INTG
16 X = 0
17 GTO 19
18 GTO 03
19 X <> Y
20 GTO 00
Find all posts by this user
Quote this message in a reply
11-30-2017, 07:50 PM
Post: #2
RE: (15C) another approach to decimal <-> binary conversion
(11-29-2017 10:32 AM)jthole Wrote:  I've written my own conversion (not using an existing program as the basis, so I surely have made beginner mistakes), which prefixes the number with a "8". That makes sure the leading zeros are preserved, and can be easily read as "B" for binary ;-)

Ah, that's what the "8" is for. #-)

(11-29-2017 10:32 AM)jthole Wrote:  To start the program, input the decimal number you want to convert (no need to press enter), and press R/S.

If the program happens to be set to step 000 or 001, that is.

The five keys A...E on the 15C are there to avoid exactly this problem. Press f[A] and the program starts at LBL A. Or B, C, D or E, respectively. That's what these keys are for. In user mode you don't even have to press the [f] prefix.

So you should not waste these useful labels A...E for simple loops or the like. Have you program start with LBL A and replace the existing LBL A with, say LBL 0 or LBL 1 or another numeric label.

(11-29-2017 10:32 AM)jthole Wrote:  The binary number is returned in the X register (in reverse).

Not if you turn the calculator upside down. This was even the "b" becomes a suffix that makes sense: 81111000 then can be read as 0001111B. :-)

And finally... what about a version that returns a result in the correct order ?-)

Dieter
Find all posts by this user
Quote this message in a reply
12-01-2017, 08:48 AM
Post: #3
RE: (15C) another approach to decimal <-> binary conversion
(11-30-2017 07:50 PM)Dieter Wrote:  And finally... what about a version that returns a result in the correct order ?-)

Dieter
Thanks for your comments, especially about label use; I clearly can learn something there (and in many other areas).

I do have the algorithm for computing the result in the right order on paper (maybe not the most efficient one, using the fact that 10^int(2log(n)) puts the "1 in the right place") but real life is interfering right now, so I'll have to code (and improve) that at a later time.

I am sure there are more efficient algorithms, and also programs for "base n to base m" conversions. So if this was a real world problem, I would do some literature search first. But the learning experience is fun Smile
Find all posts by this user
Quote this message in a reply
09-01-2018, 06:16 PM
Post: #4
RE: (15C) another approach to decimal <-> binary conversion
I've taken this generic base conversion program and hard-coded from-base=10 and to-base=2:
Code:
001 -    44  0  STO 0
002 -        8  8
003 -       34  x<>y
004 - 42,21, 0  LBL 0
005 -        1  1
006 -        0  0
007 -       34  x<>y
008 -        2  2
009 -       10  ÷
010 -    43 44  INT
011 -    43 20  x=0
012 -    22  1  GTO 1
013 -       33  R↓
014 -       20  ×
015 -       33  R↓
016 -       20  ×
017 - 44,40, 0  STO + 0
018 -       33  R↓
019 -    22  0  GTO 0
020 - 42,21, 1  LBL 1
021 -    45  0  RCL 0

Example

f CLEAR PRGM
47
R/S

101,111.0000




And here's the program for the HP-12C:
Code:
01 -    44  0  STO 0
02 -        8  8
03 -       34  x<>y
04 -        1  1
05 -        0  0
06 -       34  x<>y
07 -        2  2
08 -       10  ÷
09 -    43 25  INTG
10 -    43 35  x=0
11 - 43,33 19  GTO 19
12 -       33  R↓
13 -       20  ×
14 -       33  R↓
15 -       20  ×
16 - 44 40  0  STO+ 0
17 -       33  R↓
18 - 43,33 04  GTO 04
19 -    45  0  RCL 0



(11-30-2017 07:50 PM)Dieter Wrote:  Ah, that's what the "8" is for. #-)

It has a different meaning in my program and is actually needed.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)