The Museum of HP Calculators


AND/OR/XOR/NOT bitwise operators for the HP-32SII

This program is by Pierre GILLET and is used here by permission.

This program is supplied without representation or warranty of any kind. Pierre GILLET and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

Overview

These short programs fill a real GAP in HP-32SII, ie bitwise operators AND/OR/XOR/NOT, here on max 35 bits. Usage: First put word size in W: ex: 16 STO W For AND operator: y ENTER x XEQ A For OR operator: y ENTER x XEQ O For XOR operator: y ENTER x XEQ X For NOT operator: x XEQ N (During operation, word size is briefly displayed, for information) These operators respect normal stack behaviour like built-in functions AND/OR/XOR operators share basically the same program NOT operator uses another program (with a trick to use 35 bits) The two former programs call a "Save stack" routine which could be used in other programs.

Program

============================
Save Stack

INPUT:  Stack t,z,y,x
        Press XEQ S
OUTPUT: Stack t,z,y,x
        (T=t, Z=z, Y=y, X=x)
DESTR.: T, X, Y, Z
COMMENT:Save stack routine

Label   Checksum, bytes
S       291E, 15

LBL S   STO X   
        R↓        (→ stack x,t,z,y)
        STO Y   
        R↓        y,x,t,z
        STO Z   
        R↓        z,y,x,t
        STO T   
        R↓        t,z,y,x
        RTN     

============================
NOT x

INPUT:  W=word size (Max 35)
        Stack t,z,y,x
        Press XEQ N
OUTPUT: Stack t,z,y,NOT x
DESTR.: T, X, Y, Z
COMMENT:Operates on W bits  (Max 35)
        NOT x = 2^W-1-x     (ie 2^(W-1)-1-x+2^(W-1)
                             without overflow if W=35)
WARNING:Calls "Save stack" routine  (cf XEQ S)
        Don't forget to store word size in W

Label   Checksum, bytes
N       4643, 27

LBL N   XEQ S   Save stack
        2       2
        RCL W   2,W (Word size)
        PSE     Display Word size
        1       2,W,1
        -       2,W-1
        yx      2^(W-1)
        ENTER   2^(W-1),2^(W-1)
        RCL- X  2^(W-1),2^(W-1)-x
        1       2^(W-1),2^(W-1)-x,1
        -       2^(W-1),2^(W-1)-x-1
        +       2^(W-1)+2^(W-1)-x-1
        RCL T   2^W-1-x,t
        RCL Z   2^W-1-x,t,z
        RCL Y   2^W-1-x,t,z,y
        R↑      t,z,y,2^W-1-x
        RTN     

============================
y [AND,OR,XOR] x        

INPUT:  W=word size (max 35)
        Stack t,z,y,x
        Press [XEQ A (AND),XEQ O (OR),XEQ X (XOR)]
OUTPUT: Stack t,t,z,y [AND,OR,XOR] x
DESTR.: I, K, R, S, T, X, Y, Z, flags 1-2 (cleared)
COMMENT:Operates on W bits  (Max 35)
        Max number = 2^W-1  (W bits filled with 1)
WARNING:Calls "Save stack" routine  (cf XEQ S)
        Don't forget to store word size in W

Label   Checksum, bytes
A       B72D, 3
O       7B33, 3
X       39CF, 18
C       9672, 13,5
H       60A5, 10,5
I       E3E2, 15
G       8C7E, 6
E       38DF, 4,5
F       B5AF, 18
Total 91,5 bytes

LBL A   SF 1    AND (flags 1,2)
LBL O   SF 2    OR (flag 2)
LBL X   XEQ S   XOR (no flags)
        0       
        STO R   R=0 (result)
        2       2
        RCL W   2,W (Word size)
        PSE     Display Word size
        STO I   Counter W→1
        1       2,I,1
        -       2,I-1
        yx      2^(I-1)
        STO K   K=2^(I-1)
LBL C   0       (K=2^(I-1))
        STO S   S=0 (bit sum)
        RCL Y   Y
        RCL- K  Y-K (test bit (I-1))
        x<0?    Y-K<0?
        GTO H   → Y Bit=0
        STO Y   Y=Y-K (Y bit=1)
        ISG S   S=S+1 (i*)
LBL H   RCL X   X
        RCL- K  X-K (test bit (I-1))
        x<0?    X-K<0?
        GTO I   → X Bit=0
        STO X   X=X-K (X bit=1)
        ISG S   S=S+1 (and skip LBL !)
LBL I   RCL S   (S=bit sum)
        x=0?    S=0?
        GTO F   → R bit=0
        FS? 1   AND ? and S≠0
        GTO G   → Go further
        FS? 2   OR ? and S≠0
        GTO E   → R bit=1
        1       XOR and S≠0
        +       → (S)=(S)+1
LBL G   2       
        x≠y?    (S)≠2?
        GTO F   → R bit=0
LBL E   RCL K   (K=2^(I-1))
        STO+ R  R bit (I-1)=1
LBL F   2       
        STO÷ K  K=K/2 (next bit)
        DSE I   
        GTO C   Next bit
        CF 1    
        CF 2    Flags cleared
        RCL T   t
        RCL T   t,t
        RCL Z   t,t,z
        RCL R   t,t,z,y [AND,OR,XOR] x
        RTN     

Go back to the software library
Go back to the main exhibit hall