Previous page Next page Navigation bar

Programs

Utility Programs

Bitwise Operations

Summary Description

Programs:

BITCANIN, BITCANOUT, BITSHF, BITOP

Version:

1.0, 7 July 2002

Description:

This program performs all 16 possible bitwise operations on the binary expansion equivalents of two values.

Compatability:

Is compatible CFX-9850G

Compatible with CFX-9850G

Is compatible FX-7400G

Compatible with FX-7400G

Other programs
required:

VRPRE, VRSAVFJ, VRSAVKO, VRRESFJ, VRRESKO

Detailed Description

In certain modes, the Casio CFX-9850G calculator provides bitwise expressions (such as bitwise and, bitwise or, and bitwise not) on the binary expansion equivalents of values between -231 and 231-1. The CFX-9850G calculator does not provide these operations in general program, and the FX-7400G calculator does not provide these operations at all. Neither calculator provides bitwise shifts on the binary expansion equivalents of values. The BITOP program implements all possible Boolean operators on two values such that both calculators can use them. The BITSHF program implements bitwise shifts such that both calculators can use them.

The BITSHF program takes two values. The first value is the value to be shifted; the second value is the shift amount. The value to be shifted is first truncated to an integer, then taken modulo 232; the resulting value is then treated as if it were a bit string. The shift amount is negative for a logical right shift, and positive for a logical left shift. The shift amount is truncated to an integer.

The BITOP program takes three values. The first two values are the operands to be combined; the third is the operation code. The two operand values are first truncated to integers, then taken modulo 232; the resulting values are then treated as if they were bit strings. The operation code must be an integer between 0 and 15, inclusive; non-integer values or values outside this range are treated as zero. The operation code specifies the truth table of the operation to be applied to the two operands. The operation code is the sum of the values indicated by the following table:

Operand 1

Operand 2

Operation code value

0

0

1 if 1, 0 if 0

0

1

2 if 1, 0 if 0

1

0

4 if 1, 0 if 0

1

1

8 if 1, 0 if 0

The result is then adjusted by treating the high order bit as a sign bit: if the (unsigned) value is 231 or greater, 232 is subtracted to result in a value between -232 and 231-1.

The operation code 0 always results in 0; 15 always results in -1; 8 results in the bitwise and of Operand 1 and Operand 2; 14 results in the bitwise or of Operand 1 and Operand 2; 3 results in the bitwise not of Operand 1; and 5 results in the bitwise not of Operand 2.

This program is very similar to, but not identical to, the program of the same name presented in the Programming Tutorial. This program uses the linkage conventions described in the Linkage Conventions page.

Usage

Program BITSHF:

Load the BITSHF, BITCANIN, BITCANOUT, VRPRE, VRSAVFJ, and VRRESFJ programs into the calculator. Ensure the VRPRE program is invoked before the BITSHF program is run. Place the value to be shifted into variable A, and the shift quantity into variable B. Invoke the BITSHF program.

The BITSHF program will place the shifted value into variable C.

Program BITOP:

Load the BITOP, BITCANIN, BITCANOUT, VRPRE, VRSAVFJ, VRSAVKO, VRRESFJ, and VRRESKO programs into the calculator. Ensure the VRPRE program is invoked before the BITOP program is run. Place the two operands into variables A and B. Place the operation code into variable C. Invoke the BITOP program.

The BITOP program will place the result into variable D.

Program Source

The programs are available as a text file with .CAT contents, or may be entered as shown below. A semicolon (“;”) marks the beginning of a comment, which is not to be entered into the calculator. Remember that these programs are copyrighted; see the copyright issues page for limitations on redistribution.

Program BITSHF (240 bytes):

; Program BITSHF
;
; Description:
;   Computes the logical (zero-filled) shift of a specified value by a
;   specified amount.
;
;   The value is first truncated to an integer and taken modulo 2^32.
;   The binary expansion is then treated as a bit string. The shift
;   value is truncated to an integer and treated as a count of the
;   number of bits by which the bit string is to be shifted, and an
;   indication of the direction in which the shift is to occur.
;   Negative values correspond to right shifts; positive values cor-
;   respond to left shifts. The result is truncated to 32 bits and
;   the high order bit is treated as a sign bit, resulting in a value
;   between -2^31 and 2^31 - 1.
;
; Formal parameters:
;   A: The operand
;   B: The shift count
;   C: The result [output]
;
; Variables:
;   F: Copy of the operand
;   G: Copy of the shift count
;   H: The operand, truncated to an integer and taken modulo 2^32
;   I: The shift count, truncated to an integer
;   J: 2^32
;
; Calling sequence:
;   {operand}->A
;   {shift count}->B
;   Prog "BITSHF"
;   C->{result}
;
; Implicit inputs:
;   None
;
; Implicit outputs:
;   List 6: the variable save stack.
;   Z: the stack pointer for the variable save stack.
;
; Side effects:
;   None
;
; Symbols used:
;   -> is the assignment arrow
;   >= is the greater than or equal to relational operator
;   ^ is the exponentiation operator
;   / is the division operator
;   <= is the less than or equal to operator
;   (-) is the unary negation operator
 
Prog "VRSAVFJ"     ; Save variables F through J
A->F               ; Copy original operand value
B->G               ; Copy original shift value
Int G->I           ; Truncate shift value to an integer
If Abs I>=32       ; If the shift would make the result 0
Then 0->H          ;   Make the operand 0
0->I               ;   Make the shift value 0
Else Prog "BITCANIN"
                   ; Otherwise canonicalize the operand value
B->H               ;   and put it in H
IfEnd              ; [End of test of shift making result 0]
2^32->J            ; Get limit value into J
While I>=6         ; While left shifting at least 6 bits
64H->H             ;   Left shift by 6 bits
H-JInt (H/J)->H    ;   Take modulo 2^32
I-6->I             ;   Decrement shift count by 6
WhileEnd           ; [End of while left shifting by at least 6 bits]
While I>0          ; While left shifting at all
2H->H              ;   Left shift by 1 bit
H-JInt (H/J)->H    ;   Take modulo 2^32
I-1->I             ;   Decrement shift count
WhileEnd           ; [End of while left shifting at all]
While I<=(-)6      ; While right shifting by at least 6 bits
Int (H/64)->H      ;   Right shift by 6 bits and truncate
I+6->I             ;   Decrement shift count
WhileEnd           ; [End of while right shifting by at least 6 bits]
While I<0          ; While right shifting at all
Int (H/2)->H       ;   Right shift by 6 bits and truncate
I+1->I             ;   Decrement shift count
WhileEnd           ; [End of while right shifting at all]
H->A               ; Put result into A
Prog "BITCANOU"    ; Canonicalize result
B->C               ; Put canonical result into C
F->A               ; Restore original contents of A
G->B               ; Restore original contents of B
Prog "VRRESFJ"     ; Restore variables F through J

Program BITOP (256 bytes):

; Program BITOP
;
; Description:
;   Computes a specified bitwise operation on two values.
;
;   The two operands are first truncated to integer and taken modulo
;   2^32. Their binary expansions are then treated as bit strings. The
;   operation code encodes the truth table of the operation to be per-
;   formed, as follows:
;
;   Op 1  Op 2  Result
;     0     0     a
;     0     1     b
;     1     0     c
;     1     1     d
;
;   The operation code is the value corresponding to the binary
;   integer dcba, an integer value between 0 and 15, inclusive. If the
;   operation code is not an integer between 0 and 15 inclusive, it is
;   taken to be zero (which yields a zero result).
;
;   The value resulting from the operation is then an integer between
;   0 and 2^32. The high order bit is treated as a sign bit and the
;   value is adjusted to be between -2^31 and 2^31 - 1.
;
; Formal parameters:
;   A: The first operand
;   B: The second operand
;   C: The operation code
;   D: The result [output]
;
; Variables:
;   F: Copy of the first operand
;   G: Copy of the second operand
;   H: Copy of the operation code
;   I: The first operand, truncated to an integer and taken modulo
;      2^32
;   J: The second operand, truncated to an integer and taken modulo
;      2^32
;   K: The actual operation code
;   L: The value corresponding to the current bit
;   M: The result accumulator
;   N: The bit number of the current bit
;   O: The operation code, shifted so that its low order bit is the
;      result bit
;
; Calling sequence:
;   {operand 1}->A
;   {operand 2}->B
;   {operation code}->C
;   Prog "BITOP"
;   D->{result}
;
; Implicit inputs:
;   None
;
; Implicit outputs:
;   List 6: the variable save stack.
;   Z: the stack pointer for the variable save stack.
;
; Side effects:
;   None
;
; Symbols used:
;   -> is the assignment arrow
;   <> is the not equal to relational
;   => is the conditional jump operator
;   / is the divide operator
 
Prog "VRSAVFJ"     ; Save variables
Prog "VRSAVKO"     ;   F through O
A->F               ; Copy Op 1 into F
B->G               ; Copy Op 2 into G
C->H               ; Copy operation code into H
Prog "BITCANIN"    ; Canonicalize Op 1
B->I               ; Put canonical Op 1 into I
G->A               ; Get Op 2
Prog "BITCANIN"    ; Canonicalize Op 2
B->J               ; Put canonical Op 2 into J
H->K               ; Get operation code into K
H<>Int H=>0->K     ; If operation code is non-integer, make it 0
H<0=>0->K          ; If operation code is negative, make it 0
H>15=>0->K         ; If operation code is too large, make it 0
1->L               ; Initialize bit mask: low order bit
0->M               ; Initialize result: all bits clear
For 0->N To 31     ; Loop through all bits
K->O               ;   Get the operation code
Frac (Int (I/L)/2)<>0=>O/4->O
                   ;   If the bit is set in Op 1, shift op code
Frac (Int (J/L)/2)<>0=>O/2->O
                   ;   If the bit is set in Op 2, shift op code
Frac (Int O/2)<>0=>L+M->M
                   ;   Put the low order bit of op code into result
2L->L              ;   Adjust the bit mask for next time
Next               ; [End of loop through bits]
M->A               ; Get result
Prog "BITCANOU"    ; Canonicalize result
B->D               ; Store result
F->A               ; Restore original A contents
G->B               ; Restore original B contents
H->C               ; Restore original C contents
Prog "VRRESKO"     ; Restore variables
Prog "VRRESFJ"     ;   F through O

Program BITCANIN (91 bytes):

; Program BITCANIN
;
; Description:
;   Computes the integer portion of a value modulo 2^32.
;
; Formal parameters:
;   A: The value to be converted
;   B: The result [output]
;
; Variables:
;   F: Copy of the value
;   G: 2^32
;
; Calling sequence:
;   {value}->A
;   Prog "BITCANIN"
;   B->{result}
;
; Implicit inputs:
;   None
;
; Implicit outputs:
;   List 6: the variable save stack.
;   Z: the stack pointer for the variable save stack.
;
; Side effects:
;   None
;
; Symbols used:
;   -> is the assignment arrow
;   ^ is the exponentiation operator
;   >= is the greater than or equal to relational operator
;   / is the division operator
 
Prog "VRSAVFJ"     ; Save variables F through J
Int A->F           ; Copy input into F
2^32->G            ; Get 2^32
While Abs F>=G     ; While F has bits outside the range
F-GInt (F/G)->F    ;   Get rid of high order bits
WhileEnd           ; [End of while F has bits outside the range]
While F<0          ; While F is negative
F+G->F             ;   add in 2^32
WhileEnd           ; [End of while F is negative]
F->B               ; Put result into B
Prog "VRRESFJ"     ; Restore variables F through J

Program BITCANOU (69 bytes):

; Program BITCANOU
;
; Description:
;   Adjust a value between 0 and 2^32 to be between -2^31 and 2^31 - 1
;   by subtracting 2^32 if necessary.
;
; Formal parameters:
;   A: The value to be adjusted
;   B: The result [output]
;
; Variables:
;   F: Copy of the value
;   G: 2^31
;
; Calling sequence:
;   {value}->A
;   Prog "BITCANOU"
;   B->{result}
;
; Implicit inputs:
;   None
;
; Implicit outputs:
;   List 6: the variable save stack.
;   Z: the stack pointer for the variable save stack.
;
; Side effects:
;   None
;
; Symbols used:
;   -> is the assignment arrow
;   ^ is the exponentiation operator
;   >= is the greater than or equal to relational operator
 
Prog "VRSAVFJ"     ; Save variables F through J
A->F               ; Get value
2^31->G            ; Get 2^31
While F>=G         ; While F is too large
F-2G->F            ;   Adjust value
WhileEnd           ; [End of while F is too large]
F->B               ; Set result
Prog "VRRESFJ"     ; Restore variables F through J

[ Previous page | Top of page | Next page ]

Previous page Top of page Next page Navigation bar

Copyright © 2002 Brian Hetrick
Page last updated 14 July 2002.

Brian’s Casio Calculator Corner

Home

Programs

Index

Linkage Conventions

Mathematics

Finance

Physics

Utility

Index

Save/Restore

Line Drawing

Boolean Operations

Bitwise Operations

Tutorial

Puzzles

Site Information

Your Privacy

Site Map

E-mail

Site Technical Data