(16C) show which bits are set in a number

03042019, 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) Here's the code: Code: LBL A Thanks very much indeed for any comments. Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03042019, 02:15 AM
Post: #2




RE: (16C) show which bits are set in a number
(03042019 12:57 AM)cdmackay Wrote: I just wrote my first (!) RPN program. Welcome to the club! Quote:I'm not concerned about performance, but I would like minimise the precious resources used, e.g. registers/lines/labels. I have just removed the use of registers (besides register I): Code: 00143,22, A : ▸LBL A Example: 23 d GSB A »0 d« »1 d« »2 d« »4 d« 23 d Cheers Thomas 

03042019, 04:02 PM
Post: #3




RE: (16C) show which bits are set in a number
(03042019 02:15 AM)Thomas Klemm Wrote: I have just removed the use of registers (besides register I): oh! that's great, thanks very much indeed, Thomas. I'd forgotten about using LSTX/Rv in a program. And it saves 3 lines too thanks. Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03042019, 05:37 PM
(This post was last modified: 03042019 08:23 PM by Thomas Klemm.)
Post: #4




RE: (16C) show which bits are set in a number
(03042019 04:02 PM)cdmackay Wrote: I'd forgotten about using LSTX/Rv in a program. I'm not that familiar with the HP16C, so I was a little surprised that the B? command consumes the xregister. Since the next command is a branchinstruction, the LSTx command to recover x has to be executed twice in lines 009 and 015. Otherwise we could remove these two commands. Checkcommands like x=y don't modify the stack, so this behaviour isn't consistent. However it might be beneficial when register I is used as the index. Congratulations! That's a nice little program. I like particularly that its flow is like a backstitch when returning to the top after displaying the index. Cheers Thomas 

03042019, 06:42 PM
Post: #5




RE: (16C) show which bits are set in a number
thanks very much again, Thomas.
(03042019 05:37 PM)Thomas Klemm Wrote: I'm not that familiar with the HP16C, so I was a little surprised that the B? command consumes the xregister. I was also surprised to find that it can't usefully be used in Run mode, as far as I can see. I'd have thought a shortcut to checking whether bit n is set would be generally useful, other than solely as a branch control in a program. Without that, if you want to check bit 52, for example, you need to work out mentally that you need WINDOW 6, then count along 4 bits. That's a bit (ahem) errorprone, at least when I do it. Alternatively: "1 52 RLn X<>Y AND". Then LSTx your original number. That led me to the trivial: Code: LBL B Perhaps I'm missing something obvious Quote:I like particularly that its flow is like to a backstitch when returning to the top after displaying the index. I won't take any credit: it just fell out that way I liked that I could use #B to initialise the loop index so I stop as soon as I've found all the set bits, without having to check WSIZE bits. I was going to have it also output #B, but since the original number is preserved, the user can simply do it themselves immediately after, or do "#B LSTx" before. Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03042019, 08:22 PM
Post: #6




RE: (16C) show which bits are set in a number  
03042019, 10:35 PM
Post: #7




RE: (16C) show which bits are set in a number
(03042019 08:22 PM)Thomas Klemm Wrote: IIRC all checks executed in Run mode single step the program counter if the result is false. yup, confirmed. An odd way to check though thanks. Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03062019, 09:41 AM
Post: #8




RE: (16C) show which bits are set in a number
Here is another approach using the bit shift features of the HP16C:
Code:
Hartmut 

03062019, 06:57 PM
Post: #9




RE: (16C) show which bits are set in a number
(03062019 09:41 AM)wynen Wrote: Ah, great – one of the good old tricks from the golden days of RPN programming: have a test followed by another one that always tests false, and you get an inverse test. So the combination of F? 4 and x<0? tests if flag 4 is clear. I wouldn't have expected that anyone may still remember these things. ;) Dieter 

03062019, 10:16 PM
(This post was last modified: 03062019 10:17 PM by wynen.)
Post: #10




RE: (16C) show which bits are set in a number
But the trick is not needed and the program could be smaller
Code:
Labels: A. 0 Hartmut 

03072019, 12:45 AM
Post: #11




RE: (16C) show which bits are set in a number
Personally, I would add clear flag 4 just to be tidy
Code: 01643, 5, 4 : CF 4 // clear carry bit Sylvain 

03072019, 01:41 AM
Post: #12




RE: (16C) show which bits are set in a number
(03062019 09:41 AM)wynen Wrote: Here is another approach using the bit shift features of the HP16C: thanks very much! I'll study this tomorrow, too late tonight… Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03072019, 01:45 AM
Post: #13




RE: (16C) show which bits are set in a number
One slight inefficiency is that you test all the bits; i might add back in my use of #b so we can stop when we know we've found them all.
but I like the shifting technique, thanks Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03072019, 09:45 AM
Post: #14




RE: (16C) show which bits are set in a number
In fact your program will test all the bits beginning with LSB as well and stops when all bits set where found. This is the same behaviour as in my program.
Code: 013 43 48 : x<>0 // are there any bits left? 

03072019, 08:31 PM
Post: #15




RE: (16C) show which bits are set in a number
(03072019 09:45 AM)wynen Wrote: In fact your program will test all the bits beginning with LSB as well and stops when all bits set where found. This is the same behaviour as in my program. argh! sorry, missed that test thanks again. Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03302019, 11:33 PM
Post: #16




RE: (16C) show which bits are set in a number
Having seen mfleming's (to whom thanks) recentlyposted program to work out the current WSIZE, I realised that you get this almost for free with the program I posted here.
All that's needed — I think — is something like: Code: LBL B # WSIZE? I might change my original program so that it doesn't recall the original number, and then the above could do the addition to present the WSIZE directly. Any good? Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

03312019, 02:40 PM
Post: #17




RE: (16C) show which bits are set in a number
Although now that mfleming has updated their program to be as simple as:
CLx NOT B# that might be simpler still Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 48GX 50g 35s 30b/WP34S Prime G2 & Casios, Rockwell 18R :) 

« Next Oldest  Next Newest »

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