# HP Forums

Full Version: Bit fiddling on the HP33s
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Bit fiddling on the HP33s

One of the omissions on the 33s is bit fiddling commands - and, or, not. This is odd, since numbers in bases 16, 8, and 2 can be entered and used. So, I decided to fix this.

The routines shown here work on 32 bit unsigned integers, though any number of bits (up to 35) can be used. The method used is to get the corresponding bits of the 2 words to be operated upon, get a value from 0 to 3, and use that as a lookup in a 4 bit table. Separate tables are used for and, or, and not. Any 4 bit table can be used.

The routine uses the integer divide and remainder operations on the 33s; I do not know if they are on the 32sii.

The routine uses only integer values, so it can be run in hex, oct, or bin mode. However, you have to be in decimal mode when entering the "y^x" command.

The code uses lbl H to return the number of bits to process.

The registers used are:

```V: First operand
U: Second operand
T: 4 bit table
S: 2^k for k from 31 to 0
R: Result
Usage:
And: V U xeq A
Or: V U xeq O
Not: U xeq N
Result in X, U in Y, and V in Z (so you can roll down to get parameters)
General; V U table xeq B
And:
LBL A
8 // decimal - 1000 binary
gto B
Or:
LBL O
14 // decimal - 1110 binary
gto B
Not:
LBL N
5 // decimal - 0101 binary
gto B
Number of bits:
LBL H
16 // or 8 or 32 or ...
rtn
Bit fiddler:
LBL B
sto T // the 4 bit table
Rv // roll down
abs sto U // make it unsigned
Rv abs sto V // got the operands and table
2 xeq H 1 - y^x sto S // bit extractor
0 sto R // the eventual result
LBL C // start of the loop
rcl V rcl S int/ // integer part of V/2^k
2 rmdr 2 * // 2 times the bit
rcl U rcl S int/ 2 rmdr // same for U
+ // 2*v_bit + u_bit
2 x<>y y^x // 2^u_v value
rcl T x<>y int/ // x<>y could be saved if T loaded earlier
2 rmdr // that's the bit
rcl* S sto+ R // scale up and store
rcl S 2 int/ sto S // get next lower power of 2
x>0? gto C // if anything there, continue
rcl V rcl U rcl R // load inputs and output
rtn // done
```

Possible improvements/enhancements:

1) Get 2 bits at a time, and use a 16-bit table. This would cut the running time by about 50% - it is now about 4 seconds on a 33s.

2) have 3 or more parameters - an arbitrary truth table of up to 5 parameters (n parameters takes 2^n bits) could be handeled.

My usual statement: All comments and suggestions will be considered.

One nice thing about the 33s - you can't run out of program space - you will run out of labels first!

Martin Cohen
5/4/04

Note added later: I have written a 2-bit version, as mentioned in (1) above. If anyone is interested, I can post it.