Post Reply 
(16C) show which bits are set in a number
03-04-2019, 12:57 AM
Post: #1
(16C) show which bits are set in a number
I just wrote my first (!) RPN program.

I'm posting it here not because I think it will be of any great use to anyone but me, as it's likely naive in the extreme, but I would appreciate any criticism on how it might be improved, better RPN techniques, etc. It turned out rather more unwieldy than I'd hoped.

I'm not concerned about performance, but I would like minimise the precious resources used, e.g. registers/lines/labels.

It simply shows what bit positions are set in whatever is in X, using a single PSE to show each bit position.

When I'm decoding values, I often find myself counting bits: ah yes, bits 15 and 11 are set in that MMU register, let's see what that means. And I'm often miscounting, losing place, etc.

Alternatively, debugging C code like:
Code:
if (value & 0x00003800)
    do_one();
and having to work out: which bits are those being tested?

Here's the code:
Code:
LBL A
STO 0                           # X -> R0
#B                                # number of bits set -> RI
STO I                            #    as a decrementing loop index
RCL 0                           # original number
0                                  # start testing at bit 0
STO 1                           # bit to test
LBL 0
B?                                 # if bit is set
GTO 2                           #    jump down to show bit
LBL 1                            # else continue
RCL 1                           # incr bit to test
1
+
STO 1
GTO 0                           # jump up to re-test
LBL 2
RCL 1
PSE                             # show bit
RCL 0                           # recall original number
DSZ                              # if --RI != 0 (more bits to test?)
GTO 1                          #    jump back up
RTN                             # else Return; original number will be displayed


Register use
Index   number of bits set in original number
R0      original number
R1      bit currently being tested


Labels: A, 0–2

Thanks very much indeed for any comments.

Cambridge, UK
41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot
Casio, Rockwell 18R
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(16C) show which bits are set in a number - cdmackay - 03-04-2019 12:57 AM



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