UP | HOME

free42 Base-N Tools

Author: Mitch Richling
Updated: 2024-02-12 12:55:44

Copyright 2024 Mitch Richling. All rights reserved.

Table of Contents

1. Metadata

The home for this HTML file is: https://richmit.github.io/hp42/base.html

A PDF version of this file may be found here: https://richmit.github.io/hp42/base.pdf

Files related to this document may be found on github: https://github.com/richmit/hp42

Directory contents:

src - The org-mode file that generated this HTML document
src_42s - Ready to convert source listings for 42s code in this document
docs - This html document and associated PDF
bin - Importable RAW program files

2. Introduction

bviewSS.jpeg

When I'm doing embedded programming I use my HP-16c & DM16 calculators all the time. The programs here augment the 42's capabilities with every function on the 16c that I use. Functionality beyond the 16c includes comprehensive support for bit fields, better masks, integer square root & logarithms, and more. Finally this software provides a SHOW-like function called BVIEW is capable of displaying a full 64-bit binary number on a standard 42s display.

Everything is encapsulated into a single program with multiple global labels so that most of the functionality can be called directly from other programs.

The only thing that still bugs me is the annoying way the 42s requires one to enter hexadecimal digits. Not much I can do about that…

3. BASE: BASE-N Application

Menu:SubMenu:Key Program Description AutoBV?
S&R:SLn   Shift Y Left X Bits Y
S&R:SRn   Shift Y Right X Bits Y
S&R:RLn   Rotate Y Left by X bits Y
S&R:RRn   Rotate Y Right by X bits Y
S&R:▒▒▒▒      
S&R:BVIEW      
S&R:LJ   Left Justify Bits.  
S&R:RJ   Right Justify Bits  
S&R:SHFXY   Shift X by Y Bits (left when Y negative) Y
S&R:ROTXY     Y
S&R:▒▒▒▒      
S&R:BVIEW      
S&R:SL   Shift Left 1 Bit Y
S&R:SR   Shift Right 1 Bit Y
S&R:RL   Rotate Left 1 Bit Y
S&R:RR   Rotate Right 1 Bit Y
S&R:▒▒▒▒      
S&R:BVIEW      
BITS:GETB   Return the Xth bit of Y  
BITS:SB SETB Set the Xth bit of Y Y
BITS:CB CLRB Clear the Xth bit in Y Y
BITS:AB ASNB Assign X to the Yth bit of Z Y
BITS:▒▒▒▒      
BITS:BVIEW      
BITS:GY@X   Return the Y bits of Z starting at bit X Y
BITS:SY@X   Set Y bits of Z starting at bit X Y
BITS:CY@X   Clear Y bits of Z starting at bit X Y
BITS:AZ@Y   Assign LS Z bits of X to Z bits of T starting at Y Y
BITS:▒▒▒▒      
BITS:BVIEW      
BITS:POPRB   Pop rightmost bits X bits off of Y Y
BITS:POPLB   Pop leftmost bits X bits off of Y Y
BITS:▒▒▒▒      
BITS:▒▒▒▒      
BITS:▒▒▒▒      
BITS:BVIEW      
FUN:B#   Count set bits  
FUN:MSSB   Position of the Most Significant Set Bit  
FUN:LSSB   Position of the Least Significant Set Bit  
FUN:ILN2   Integer base 2 log  
FUN:ISQRT   Integer square root  
FUN:BVIEW      
FUN:REVNB   Reverse rightmost X bits of Y Y
FUN:REVB   Reverse bits of X Y
FUN:▒▒▒▒      
FUN:▒▒▒▒      
FUN:▒▒▒▒      
FUN:BVIEW      
MASK:MSKL   Create integer with left most X bits set Y
MASK:MSKR   Create integer with right most X bits set Y
MASK:MSKn   Create integer with Y set bits located at bit X Y
MASK:▒▒▒▒      
MASK:▒▒▒▒      
MASK:BVIEW      
ARTH:AND     Y
ARTH:OR     Y
ARTH:XOR     Y
ARTH:NOT     Y
ARTH:NEG BASE+/-   Y
ARTH:BVIEW      
ARTH:+ BASE+   Y
ARTH:- BASE-   Y
ARTH:× BASE×   Y
ARTH:÷ BASE÷   Y
ARTH:MOD     Y
ARTH:BVIEW      
BVIEW      
LBL 92 HEXM Set current base to hexadecimal  
LBL 93 DECM Set current base to decimal  
LBL 94 OCTM Set current base to octal  
LBL 95 BINM Set current base to binary  
▒▒▒▒ -    
BMNU LBL 75 Switch to built in BASE menu  
LBL 96 WSIZE Set WSIZE  
LBL 97 BSIGNED Toggle signed/unsigned mode  
LBL 98 BWRAP Toggle wrapped mode  
▒▒▒▒ -    
LBL 99 LBL 74 Toggle BVIEW padding (Flag 02)  
▒▒▒▒ -    

3.2. Menu Code

The menu program is generated via the following bit of elisp. You must first define the MJR-generate-42-menu-code and MJR-custom-x-gen by evaluating the code blocks in the hp42s-meta.org file. The skeleton was generated by the following code. That skeleton has been fleshed out with some custom code.

(MJR-generate-42-menu-code "BASE" 0 tbl 0 1 'stay 'up 'auto #'MJR-custom-gen-lab #'MJR-custom-gen-sub)

3.3. Application Local Subroutines

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ref:b#)
@@@@ DSC: Number of bits set
@@@@ IN:  X: an integer
@@@@ OUT: X: Number of 1 bits in IP(X)
@@@@ FAQ: Runtime complexity O(log_2(X))
@@@@ LBL: Used 70-72
@@@@ UPD: 2021-04-10
@@@@ LBL "B#"
LBL "B#"
FUNC 11    @@## REQ:free42>=2.5.24
L4STK      @@## REQ:free42>=3.0
IP
0
X<>Y       @@@@ NUM CNT
1
NOT
X<>Y       @@@@ NUM MASK CNT
LBL 70
RCL ST Y   @@@@ MASK NUM MASK CNT
X<>Y       @@@@ NUM MASK MASK CNT
AND        @@@@ NUM_N MASK CNT
LASTX      @@@@ NUM NUM_N MASK CNT
X=Y?
GTO 71
@@@@ Current bit was set: increment counter
R↓         @@@@ NUM_N MASK CNT
1 
STO+ ST T  @@@@ 1 NUM_N MASK CNT
LBL 71
@@@@ Current bit was clear
R↓         @@@@ NUM_N MASK CNT
X=0?
GTO 72  
@@@@ Still have bits to check
X<>Y       @@@@ MASK NUM_N CNT
-1
ROTXY
X<>Y       @@@@ NUM_N MASK CNT
GTO 70
LBL 72
@@@@ No bits left to check
R↓         @@@@ MASK CNT
R↓         @@@@ CNT
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MSKn)
@@@@ DSC: Create integer with Y set bits located at bit X
@@@@ IN:  Y: An integer
@@@@ IN:  X: An integer (LSB=0)
@@@@ OUT: X: Integer with IP(X) least significant bits set
@@@@ FAQ: Returns int with all 0 bits when X<=0
@@@@ FAQ: Returns int with all 1 bits when X>=WSIZE?
@@@@ UPD: 2021-03-20
LBL "MSKn"
FUNC 21                        @@## REQ:free42>=2.5.24
L4STK                          @@## REQ:free42>=3.0
X<>Y
XEQ "MSKR"
X<>Y
XEQ "SLn"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MSKL)
@@@@ DSC: Create integer with left most X bits set
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer with IP(X) most significant bits set
@@@@ FAQ: Returns int with all 0 bits when X<=0
@@@@ FAQ: Returns int with all 1 bits when X>=WSIZE?
@@@@ UPD: 2021-03-20
LBL "MSKL"
FUNC 11                        @@## REQ:free42>=2.5.24
L4STK                          @@## REQ:free42>=3.0
XEQ "MSKR"
XEQ "LJ"
R↓
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MSKR)
@@@@ DSC: Create integer with right most X bits set
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer with IP(X) least significant bits set
@@@@ FAQ: Returns int with all 0 bits when X<=0
@@@@ FAQ: Returns int with all 1 bits when X>=WSIZE?
@@@@ UPD: 2021-03-20
LBL "MSKR"
FUNC 11                        @@## REQ:free42>=2.5.24
L4STK                          @@## REQ:free42>=3.0
IP
@@@@ All zeros case?
0
0≥? ST Y
RTN
R↓
0
NOT
@@@@ All ones case?
XEQ 67 @@@@ Y≥WSIZE?
RTN
R↓
@@@@ Some ones case
2
X<>Y
Y↑X
1
-
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SETB)
@@@@ DSC: Set the Xth bit of Y
@@@@ IN:  Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: X: Integer with IP(X)th bit of Y set
@@@@ FAQ: Y is returned unchanged  when X is out of range
@@@@ BUG: When X out of range, the type Y is not error checked
@@@@ UPD: 2021-03-20
LBL "SETB"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<>Y
@@@@ Do nothing if X was negative
0>? ST Y
RTN
@@@@ Do nothing if X was >= WSIZE
XEQ 67 @@@@ Y≥WSIZE?
RTN
@@@@ X must have been >= 0 & < WSIZE
X<>Y
1
X<>Y
+/-
ROTXY
OR
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CLRB)
@@@@ DSC: Clear the Xth bit in Y
@@@@ IN:  Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: X: Integer with IP(X)th bit of Y set
@@@@ FAQ: Y is returned unchanged  when X is out of range
@@@@ BUG: When X out of range, the type Y is not error checked
@@@@ UPD: 2021-03-20
LBL "CLRB"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<>Y
IP
@@@@ Do nothing if X was negative
0>? ST Y
RTN
@@@@ Do nothing if X was >= WSIZE
XEQ 67 @@@@ Y≥WSIZE?
RTN
@@@@ X must have been >= 0 & < WSIZE
X<>Y
1
X<>Y
+/-
ROTXY
NOT
AND
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (GETB)
@@@@ DSC: Return the Xth bit of Y
@@@@ IN:  Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: The IP(X)th bit of Y
@@@@ FAQ: Returns 0 when X is out of range
@@@@ BUG: When X out of range, type Y is not error checked
@@@@ LBL: Used 69
@@@@ UPD: 2021-03-20
LBL "GETB"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
0
@@@@ Return 0 if X was negative
0>? ST Y
RTN
@@@@ Return 0  if X was >= WSIZE
XEQ 67 @@@@ Y≥WSIZE?
RTN
@@@@ X must have been >= 0 & < WSIZE
R↓
BIT?
GTO 69
0
RTN
LBL 69
1
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ASNB)
@@@@ DSC: Set the Yth bit of Z to X
@@@@ IN:  Z: An integer
@@@@ IN:  Y: An integer (0=LSB)
@@@@      X: An integer
@@@@ OUT: Set the IP(Y)th bit of IP(Z) to IP(X)
@@@@ UPD: 2021-03-20
LBL "ASNB"
FUNC 31                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
X=0?
GTO 54
R↓
XEQ "SETB"
RTN
LBL 54
R↓
XEQ "CLRB"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (GYaX)
@@@@ DSC: Return the Y bits of Z starting at bit X
@@@@ EX:  Z: 111000 Y: 3 X: 2 -> X: 110
@@@@ IN:  Z: An integer
@@@@      Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: IP(Y) bits of IP(Z) starting at bit IP(X)
@@@@ UPD: 2021-04-21
LBL "GY@X"
FUNC 31                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
RCL ST Z   @@@@ Z     X     Y     Z
X<>Y       @@@@ X     Z     Y     Z
XEQ "SRn"  @@@@ SHF_Z Y     Z     Z
X<>Y       @@@@ Y     SHF_Z Z     Z
XEQ "MSKR" @@@@ MASK  SHF_Z Z     Z
AND        @@@@ BITS  Z     Z     Z
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CYaX)
@@@@ DSC: Clear Y bits of Z starting at bit X
@@@@ EX:  Z: 111000 Y: 3 X: 2 -> X: 100000
@@@@ IN:  Z: An integer
@@@@      Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: IP(Z) with IP(Y) bits starting at bit IP(X) cleared
@@@@ UPD: 2021-04-21
LBL "CY@X"
FUNC 31                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
XEQ "MSKn"
NOT
AND
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SYaX)
@@@@ DSC: Set Y bits of Z starting at bit X
@@@@ EX:  Z: 111000 Y: 3 X: 2 -> X: 111100
@@@@ IN:  Z: An integer
@@@@      Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: IP(Z) with IP(Y) bits starting at bit IP(X) set
@@@@ UPD: 2021-04-21
LBL "SY@X"
FUNC 31                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
XEQ "MSKn"
OR
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (AZaY)
@@@@ DSC: Place Z bits of X into T starting at bit Y
@@@@ EX:  T: 111000 Z: 3 Y: 2 X: 011 -> X: 101100
@@@@ IN:  T: An integer
@@@@ IN:  Z: An integer
@@@@      Y: An integer
@@@@      X: An integer (0=LSB)
@@@@ OUT: IP(Z) bits of IP(T) are set to IP(Z) bits of IP(X) starting at IP(Y)
@@@@ UPD: 2021-04-21
LBL "AZ@Y"
FUNC 41                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
LSTO "_X"
R↓              @@@@  Y Z T T
LSTO "_Y"
R↓              @@@@  Z T T T
LSTO "_Z"     @@@@  Z T T T
RCL "_Y"      @@@@  Y Z T T
XEQ "MSKn"
NOT
AND             @@@@ TM T T T
RCL "_X"
RCL "_Z"
XEQ "MSKR"
AND
RCL "_Y"
XEQ "SLn" 
OR              @@@@
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SL)
@@@@ DSC: Shift Left 1 Bit
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer shifted left 1 bit
@@@@ UPD: 2021-03-20
LBL "SL"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
-1
ROTXY
1 
NOT
AND
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SR)
@@@@ DSC: Shift Right 1 Bit
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer shifted right 1 bit
@@@@ UPD: 2021-03-20
LBL "SR"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
1
NOT
AND
1
ROTXY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (RL)
@@@@ DSC: Rotate Left 1 Bit
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer rotated left 1 bit
LBL "RL"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
-1
ROTXY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (RR)
@@@@ DSC: Rotate Right 1 Bit
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer rotated right 1 bit
LBL "RR"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
1
ROTXY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SLn)
@@@@ DSC: Shift Y Left X Bits
@@@@ IN:  Y: An integer
@@@@ IN:  X: An integer
@@@@ OUT: X: IP(Y) shifted left IP(Y) bit(s)
@@@@ FAQ: IP(Y) is returned unchanged when IP(X) is zero or negative
@@@@ FAQ: 0 is returned when IP(X)>=WSIZE
@@@@ UPD: 2021-03-20
LBL "SLn"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<>Y
IP
@@@@ Return Y if X was negative
0≥? ST Y
RTN
X<>Y
0
@@@@ Return 0 if X was >= WSIZE
XEQ 67 @@@@ Y≥WSIZE?
RTN
@@@@ X must have been > 0 & < WSIZE
R↓
X<>Y
RCL ST Y  
@@@@ Rotate left X bits
+/-
ROTXY
@@@@ Zap the right most X bits
2
RCL ST Z
Y↑X
1
-
NOT
AND
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SRn)
@@@@ DSC: Shift Y Right X Bits
@@@@ IN:  Y: An integer
@@@@ IN:  X: An integer
@@@@ OUT: X: IP(Y) shifted right IP(X) bit(s)
@@@@ FAQ: IP(Y) is returned unchanged when IP(X) is zero or negative
@@@@ FAQ: 0 is returned when IP(X)>=WSIZE
@@@@ UPD: 2021-03-20
LBL "SRn"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<>Y
IP
@@@@ Return Y if X was negative
0≥? ST Y
RTN
X<>Y
0
@@@@ Return 0 if X was >= WSIZE
XEQ 67 @@@@ Y≥WSIZE?
RTN
@@@@ X must have been > 0 & < WSIZE
R↓
@@@@ Zap the right most X bits
X<>Y
RCL ST Y
2
X<>Y
Y↑X
1
-
NOT
AND
@@@@ Rotate right X bits
X<>Y
ROTXY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (RLn)
@@@@ DSC: Rotate Y Left by X bits
@@@@ IN:  Y: An integer
@@@@ IN:  X: An integer
@@@@ OUT: X: X rotated left IP(Y) bit(s)
@@@@ FAQ: Y is returned unchanged when X is zero or negative
@@@@ UPD: 2021-03-20
LBL "RLn"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<>Y
@@@@ Return Y if X was negative
0≥? ST Y
RTN
X<>Y
@@@@ X was positive
+/-
ROTXY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (RRn)
@@@@ DSC: Rotate Y Right by X bits
@@@@ IN:  Y: An integer
@@@@ IN:  X: An integer
@@@@ OUT: X: X rotated right ABS(IP(Y)) bit(s)
@@@@ FAQ: Y is returned unchanged when X is zero or negative
@@@@ UPD: 2021-03-20
LBL "RRn"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<>Y
@@@@ Return Y if X was negative
0≥? ST Y
RTN
X<>Y
@@@@ X was positive
ROTXY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SHFXY)
@@@@ DSC: Shift X by Y Bits (left when Y negative)
@@@@ IN:  Y: An integer
@@@@ IN:  X: An integer
@@@@ OUT: X: Integer shifted by IP(Y) bit(s)
@@@@ FAQ: Uses SLn & SLr.
@@@@ LBL: Used 59
@@@@ UPD: 2021-03-20
LBL "SHFXY"
FUNC 21                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
X<0?
GTO 59
@@@@ X is non-negative -- shift right
XEQ "SRn"
RTN
LBL 59 
@@@@ X negative -- shift left
+/-
XEQ "SLn"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (RJ)
@@@@ DSC: Right Justify Bits
@@@@ IN:  X: an integer
@@@@ OUT: Y: Number of shifts required to justify
@@@@ OUT: X: Justified number
@@@@ FAQ: Like the HP-16c's LJ funciton, but justifies in the other direction
@@@@ LBL: Used 56-58
@@@@ UPD: 2021-03-20
LBL "RJ"
FUNC 12                        @@## REQ:free42>=2.5.24
L4STK                          @@## REQ:free42>=3.0
IP
0
X<>Y
LBL 57
0
BIT?
GTO 56
GTO 58
LBL 56   @@@@ LSB is one -- DONE!
R↓
X<>Y
RTN
LBL 58   @@@@ LSB is zero
R↓
1
STO+ ST Z
ROTXY
GTO 57

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (LJ)
@@@@ DSC: Left Justify Bits
@@@@ IN:  X: an integer
@@@@ OUT: Y: Number of shifts required to justify
@@@@ OUT: X: Justified number
@@@@ FAQ: Just like the HP-16c's LJ funciton
@@@@ LBL: Used 64-66
@@@@ UPD: 2021-03-20
LBL "LJ"
FUNC 12                        @@## REQ:free42>=2.5.24
L4STK                          @@## REQ:free42>=3.0
IP
0
X<>Y
LBL 64
WSIZE?
1
-
BIT?
GTO 65
GTO 66
LBL 65   @@@@ MSB is one -- DONE!
R↓
X<>Y
RTN
LBL 66   @@@@ MSB is zero
R↓
1
STO+ ST Z
+/-
ROTXY
GTO 64

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ISQRT)
@@@@ DSC: Integer square root
@@@@ IN:  X: a non-negative real number
@@@@ OUT: X: IP(SQRT(ABS(X)))
@@@@ UPD: 2021-03-16
LBL "ISQRT"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
ABS
SQRT
IP
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ILN2)
@@@@ DSC: Integer base 2 log
@@@@ IN:  X: a positive real number
@@@@ OUT: X: IP(log_2(ABS(X)))
@@@@ UPD: 2021-03-16
LBL "ILN2"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
ABS
LN
2
LN
÷
IP
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MSSB)
@@@@ DSC: Position of the Most Significant Set Bit
@@@@ IN:  X: An integer
@@@@ OUT: X: Position of MSSB in IP(X) or -1 if IP(X) was 0
@@@@ USE: LJ
@@@@ UPD: 2021-03-16
@@@@ FAQ: Runtime Complexity O(WSIZE-log_2(X))
@@@@ FAQ: Closely related to LJ program
LBL "MSSB"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
-1
0=? ST Y
RTN
R↓
XEQ "LJ"
1
+
WSIZE?
X<>Y
-
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (LSSB)
@@@@ DSC: Position of the Least Significant Set Bit
@@@@ IN:  X: An integer
@@@@ OUT: X: Position of LSSB in IP(X) or -1 if IP(X) was 0
@@@@ UPD: 2021-03-16
@@@@ FAQ: Runtime Complexity O(LSSB(X)) & LSSB<WSIZE
@@@@ FAQ: Closely related to RJ program
LBL "LSSB"
FUNC 11                       @@## REQ:free42>=2.5.24
L4STK                         @@## REQ:free42>=3.0
IP
-1
0=? ST Y
RTN
R↓
XEQ "RJ"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: WSIZE
LBL 96
"W:"
WSIZE?
AIP
R↓
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: BSIGNED
LBL 97
FS? 78
"S:+/-"
FC? 78
"S:+"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: BWRAP
LBL 98
FS? 79
"BWR•"
FC? 79
"BWRP"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: BVP (BVIEW Padding)
LBL 99
FC? 02
"BVP•"
FS? 02
"BVP"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: HEX
LBL 92
"HEX"
FS? 71
"•"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: DEC
LBL 93
"DEC"
SF 81
FS? 71
CF 81
FS? 70
CF 81
FS? 68
CF 81
FS? 81
"•"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: OCT
LBL 94
"OCT"
SF 81
FS? 71
CF 81
FC? 70
CF 81
FS? 81
"•"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Label: BIN
LBL 95
"BIN"
SF 81
FC? 68
CF 81
FS? 69
CF 81
FS? 81
"•"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Toggle 02
LBL 74
FS?C 02
RTN
SF 02
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Menu Action BINM
LBL 75
EXITALL
"Press R/S To"
" Return"
AVIEW
HEXM
STOP
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Test if Y<WSIZE
@@@@ NAM: Y≥WSIZE? 67
@@@@ I/O: No stack change.  Uses Y
@@@@ RET: YES if Y≥WSIZE, NO otherwise
@@@@ UPD: 2021-04-21
LBL 67
FUNC 00
L4STK
X<>Y
WSIZE?
X>Y?
RTNNO
RTNYES

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (REVBIT)
@@@@ DSC: Reverse Bits rightmost X bits of Y
@@@@ NAM: REVBIT
@@@@ IN:  Y: An integer
@@@@      X: An integer
@@@@ OUT: X: An integer
@@@@ UPD: 2021-04-21
LBL "REVNB"
FUNC 11
L4STK
ENTER           @@@@ X X Y
XEQ "MSKR"      @@@@ M X Y
X<>Y            @@@@ X M Y
1
-
LSTO "_MBC"
1000
÷
LSTO "_CTR"
R↓              @@@@ M Y
RCL ST Y        @@@@ Y M Y
OR              @@@@ N Y
X<>Y            @@@@ OLD NEW 
LBL 51
RCL "_CTR"       @@@@ CTR OLD NEW 
IP
BIT?
GTO 52
R↓
X<>Y
RCL "_CTR"
IP
RCL- "_MBC"
1
X<>Y
ROTXY
NOT
AND
X<>Y
ENTER
LBL 52
R↓
ISG "_CTR"
GTO 51
X<>Y
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (REVBIT)
@@@@ DSC: Reverse all Bits of X
@@@@ NAM: REVBIT
@@@@ IN:  X: An integer
@@@@ OUT: X: An integer
@@@@ UPD: 2021-04-21
LBL "REVB"
FUNC 11
L4STK
WSIZE?
XEQ "REVNB"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (POPRB)
@@@@ DSC: Shift rihtmost X bits off Y.
@@@@ IN:  Y: An integer
@@@@      X: An integer (number of bits)
@@@@ OUT: Y: Part of Y left
@@@@      X: Part of Y shifted off
LBL "POPRB"
FUNC 22
L4STK
RCL ST Y     @@@@ Y   X   Y   ?
RCL ST Y     @@@@ X   Y   X   Y
XEQ "SRn"    @@@@ LFT X   Y   Y
RCL ST Z     @@@@ Y   LFT X   Y
RCL ST Z     @@@@ X   Y   LFT X
XEQ "MSKR"   @@@@ MSK Y   LFT X
AND          @@@@ POP LFT X   Y
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (POPLB)
@@@@ DSC: Shift leftmost X bits off Y.
@@@@ IN:  Y: An integer
@@@@      X: An integer (number of bits)
@@@@ OUT: Y: Part of Y left
@@@@      X: Part of Y shifted off
LBL "POPLB"
FUNC 22
L4STK
RCL ST Y     @@@@ Y   X   Y   ?
RCL ST Y     @@@@ X   Y   X   Y
XEQ "SLn"    @@@@ LFT X   Y   Y
RCL ST Z     @@@@ Y   LFT X   Y
RCL ST Z     @@@@ X   Y   LFT X
XEQ "MSKL"   @@@@ MSK Y   LFT X
AND          @@@@ PS  LFT X   Y
RCL ST Z
+/-
ROTXY        @@@@ POP LFT X   Y
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (BVIEW)
@@@@ DSC: Print binary numbers (up to 64-bit)
@@@@ IN:  X: An integer
@@@@ OUT: Nothing -- prints to screen
@@@@ FLG: 81: TEMP: Set: We are on DM42;            Clear: We are not on DM42
@@@@ FLG: 01: PREF: Reserved
@@@@ FLG: 02: PREF: Set: BVIEW prints WSIZE digits; Clear: BVIEW prints 64 digits
@@@@ FAQ: If XEQ while system BASE menu is active, returns to same menu.
@@@@ BUG: Sometimes gets the system base menu to return to wrong. ;)
@@@@ UPD: 2021-03-20
LBL "BVIEW"
FUNC 11              @@## REQ:free42>=2.5.24
L4STK                @@## REQ:free42>=3.0
0
SF 25
BIT?
FS?C 25
GTO 79
R↓
"ERR: Bad Int!"
AVIEW
RTN
LBL 79
R↓
LSTO "_X"
20
1
FS? 68
STO+ ST Y
FS? 70
STO+ ST Y
FS? 71
STO+ ST Y
R↓             @@@@ 20:DECN 21:BINM 22:OCTM 23:HEXM
LSTO "_SBMD"
R↓
CF 81
SF 25
RCL "GrMod"
FS?C 25
SF 81
FC? 81
GTO 88
@@@@ We are on a DM42
0
STO "GrMod"     @@@@ Set graphics mode to 42 classic on DM42
R↓
LBL 88
CLLCD
EXITALL
63
1000
÷
LSTO "_CTR"
LBL 76
@@@@ Figure out X&Y coordinates for digit
RCL "_CTR"            @@@@ IF
IP
32
X>Y?
GTO 73
GTO 83
LBL 73               @@@@ IF-THEN FIRST ROW
1
RCL "_CTR"
4
×
1
+
GTO 90
LBL 83               @@@@ IF-ELSE SECOND ROW
9
RCL "_CTR"
32
-
4
×
1
+
LBL 90               @@@@ IF-END
@@@@ Figure out current bit value
63
RCL "_CTR"            @@@@ IF-BEGIN
IP
-
WSIZE?
X>Y?
GTO 80
GTO 81
LBL 80               @@@@ IF-THEN
R↓
+/-                  @@@@ IF-BEGIN
1
X<>Y
ROTXY
RCL "_X"
AND
X=0?
GTO 85
GTO 86
LBL 85               @@@@ IF-THEN bit is 0
R↓
0
GTO 87
LBL 86               @@@@ IF-ELSE bit is 1
R↓
1
LBL 87
LSTO "_CB"
GTO 82
LBL 81               @@@@ IF-ELSE
R↓
R↓
@@@@ Bit beyond WSIZE padding character
0
LSTO "_CB"
FS? 02
GTO 84
LBL 82               @@@@ IF-END
R↓
@@@@ Figure out grouping
RCL "_CTR"            @@@@ IF-START grouping
IP
4
÷
IP
2
÷
FP
X=0?
GTO 55
GTO 68
LBL 55               @@@@ IF-THEN
R↓
0
GTO 77
LBL 68               @@@@ IF-ELSE
R↓
2
LBL 77               @@@@ IF-END
@@@@ Compute digit character code
RCL+ "_CB"
@@@@ Draw it
XEQ 78
LBL 84
ISG  "_CTR"
GTO 76
FC? 81
GTO 89
@@@@ @@@@ On DM42.  Hack to keep screen clean
@@@@ GETKEY
@@@@ CLLCD
@@@@ "BVIEW FINISHED"
@@@@ AVIEW
LBL 89
RCL "_SBMD"
20
X=Y?
DECM
R↓
21
X=Y?
BINM
R↓
22
X=Y?
OCTM
R↓
23
X=Y?
HEXM
R↓
@@@@ Recall original X
RCL "_X"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: tiny binary digit print
@@@@ IN:  Z: Y coordinate for upper left point of character -- Top of screen is 1
@@@@      Y: X coordinate for upper left point of character -- Left of screen is 1
@@@@      X: Character number
@@@@ OUT: No return
@@@@ UPD: 2021-04-11
@@@@ BUG: Characters can be *VERY* tiny in high resolution modes on DM42
@@@@ Characters can be underlined
@@@@ Characters are 3x5 pixels in size.  Underlined characters are 3x7.
@@@@   - Stock HP-42s screen: 32 characters across.  Two full lines on the screen.
@@@@ Non-Underlined Character numbers:
@@@@     00 01
@@@@      0  1
@@@@ Add 2 to the above character number for the underlined version
LBL 78
FUNC 30      @@## REQ:free42>=2.5.24
L4STK        @@## REQ:free42>=3.0
IP
60
+
XEQ IND ST X
R↓
AGRAPH
RTN
LBL 60       @@@@ CHAR: 0
"•μ•"        @@@@ #b11111 #b10001 #b11111
RTN
LBL 61       @@@@ CHAR: 1
"£•←"        @@@@ #b10010 #b11111 #b10000
RTN
LBL 62       @@@@ CHAR: 0
"_Q_"        @@@@ #b1011111 #b1010001 #b1011111
RTN
LBL 63       @@@@ CHAR: 1
"R_P"        @@@@ #b1010010 #b1011111 #b1010000
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
END

4. EOF