Post Reply 
integer logarithm on the HP-16C
01-29-2024, 04:14 AM
Post: #1
integer logarithm on the HP-16C
I use the HP-16C quite a bit, and one thing I miss is something I grew up calling the "integer logarithm". It's simply the floor of the base 2 logarithm -- equal to the index of the most significant set bit. So here is my version:

Code:
LBL A
x=0?
/
LJ
R^
1
LJ
R^
-
RTN

It runs in about 4 seconds on my original 16C and is instantaneous on the DM-16L. Note that it wipes out the entire contents of the stack. An "Error 0" is produced for a zero argument.

I'm curious how others would implement this function. My first attempt was to shift until a zero was found, but that took a LONG time to run on the original 16C.

Note you can get CTZ (Count Trailing Zeros) from this function like so:

Code:
ENTER
ENTER
1
-
NOT
AND
GSB A
Find all posts by this user
Quote this message in a reply
Post Reply 




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