(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
 jthole Member Posts: 60 Joined: Nov 2017
(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!

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
 « Next Oldest | Next Newest »

 Messages In This Thread (15C) another approach to decimal <-> binary conversion - jthole - 11-29-2017 10:32 AM RE: (15C) another approach to decimal <-> binary conversion - Dieter - 11-30-2017, 07:50 PM RE: (15C) another approach to decimal <-> binary conversion - jthole - 12-01-2017, 08:48 AM RE: (15C) another approach to decimal <-> binary conversion - Thomas Klemm - 09-01-2018, 06:16 PM

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