HP Forums
integer logarithm on the HP-16C - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: integer logarithm on the HP-16C (/thread-21215.html)



integer logarithm on the HP-16C - richmit - 01-29-2024 04:14 AM

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