# Calculator Programming Tutorial

## Programming Building Blocks I

### Examples

#### Logical Operations for the Calculator

##### Program Analysis

The problem as it now stands is to program logical operations on the Casio FX-7400G. There are two types of logical operations: unary and binary. There are four possible unary operations, as follows:

A

Operation 0

Operation 1

Operation 2

Operation 3

False

False

True

False

True

True

False

False

True

True

Let us see if we can find more descriptive names than “operation 0” through “operation 3.” Operation 0 is always false, regardless of the value of A: we can call this operation, “False.” Operation 1 is the opposite of A: we can call this operation “Not.” Operation 2 repeats the value of A: we can call this operation “A,” or “no-op” (short for “no operation”). Operation 3 is always true, regardless of the value of A: we can call this operation, “True.”

Providing True, False, and No-Op seem relatively senseless. Actually, providing No-Op might make sense. If we consider A as False or True as it is zero or non-zero, but we always generate 0 and 1 for False and True, then No-Op would actually map A into the generated False and True values.

Now consider the possible binary operators. There are 16 of these, as follows:

A

B

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

F

F

F

T

F

T

F

T

F

T

F

T

F

T

F

T

F

T

F

T

F

F

T

T

F

F

T

T

F

F

T

T

F

F

T

T

T

F

F

F

F

F

T

T

T

T

F

F

F

F

T

T

T

T

T

T

F

F

F

F

F

F

F

F

T

T

T

T

T

T

T

T

This time, operation 0 is “False,” and operation 15 is “True.” Operation 1 is nor (not or), 2 is not (B imp A), 3 is not A, and 4 is not (A imp B). Operation 5 is not B, 6 is xor, 7 is nand (not and), and 8 is and. Operation 9 is eqv (or not xor), 10 is B, 11 is A imp B, and 12 is A. Operation 13 is B imp A, and 14 is or.

A curious thing, perhaps, is that all unary operations upon A, and all unary operations upon B, are binary operations in A and B. A little thought, however, shows this is only natural. We have listed all 16 possible binary operations, including those that depend on only one operand, or indeed on no operand at all (True and False).

The next step is to determine what to do with this information. We can write 20 separate subprograms: 4 for unary operations, 16 for binary operations. We can subsume the unary operations in the binary operations, and write 16 separate subprograms for the binary operations. We can find out which particular logical operations are desired, and write subprograms only for those. We can write one subprogram that can do multiple binary operations.

Hmm. That last one seems best: we can write one subprogram that does it all. But the program would be an absolute mass of If statements, based on some sort of parameter telling us which operation is to be performed. We could simplify the subprogram by implementing only a select few operations, say Not, Or, and And. We can rough out our first attempt at the subprogram as follows:

```if κ = 0 then
if α ≠ 0 then
ρ := 0
else
ρ := 1
else if κ = 1 then
if α ≠ 0 then
ρ := 1
else if β ≠ 0 then
ρ := 1
else
ρ := 0
else
if α ≠ 0 then
if β ≠ 0 then
ρ := 1
else
ρ := 0
else
ρ := 0;
return ρ;
```

where α and β are the two logical arguments, and κ is an operation code (0 for not α, 1 for α or β, and 2 for α and β). (It is traditional, incidentally, for pseudocode to look very much like publication ALGOL, the International ALGOrithmic Language. ALGOL, first formalized in 1960, was a great advance over many of its successors. The above pseudocode looks quite a bit like publication ALGOL.)

Well, this works, but it is not particularly satisfactory. There is no unity: sometimes the subprogram is unary, sometimes binary, and there are repeated tests of a and b, and there is no particular meaning attached to the code indicator. We need to think a little more.

Looking at the truth table for the binary operators, we realize that the Fs and Ts are binary states: 0 and 1. Further, putting all the 0s and 1s together, we are counting in binary. The function result column is just the binary expansion of the operation number! And if we put 0s and 1s in the A and B columns, then AB together is just the binary expansion of the row number!

Oh! Light dawns over marble head!

If the operation indicator κ is just the function number, then AB tells us which bit of the function number is the function result. AB is 2a+b, where a is 0 or 1 as A is false or true, and b is 0 or 1 as B is false or true. And one way to extract the I-th bit of a binary number is to right shift by I bits, then take the bottommost bit.

So if we have κ as a function number, then we right shift by 2 bits if A is true, right shift by 1 bit if B is true, and the bottommost bit is the function result. This gives us much simpler logic:

```if α ≠ 0 then
κ := κ div 4;
if β ≠ 0 then
κ := κ div 2;
return κ mod 2;
```

Well, I think we can program this relatively easily. The integer divide operator div is a divide, followed by a truncation to an integer value. The integer modulus operator mod can be formulated several ways. The most convenient way on the FX-7400G calculator is a divide, followed by discarding the integer portion, followed by a multiplication.

Page last updated 27 January 2002.

# Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Data Types

Numbers

Variables

Expressions

Control Flow I

Control Flow II

Subprograms

Basic I/O

Algorithms

A First Program

Examples

Introduction

Logical Ops

Problem

Program

Code

Test

Bitwise Ops

Pumpkins

Exercises

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card

References

Puzzles

Site Information