# Calculator Programming Tutorial

## Programming Building Blocks I

### Examples

#### Bitwise Operations for the Calculator

##### Problem Statement

The problem statement presented to us is:

The Casio FX-7400G calculator does not support bitwise operations, and the Casio CFX-9850G calculator supports them only in a base conversion program. Provide them.

Validating the problem, we learn that the FX-7400G indeed does not provide bitwise operations, and the CFX-9850G indeed provides them to base conversion programs but not to normal programs. The bitwise operations provided by the CFX-9850G are negate, not, and, or, xor, and xnor, whatever xnor is. Negate is just our old friend negative. The truth tables for these functions are:

A

B

Not A

A and B

A or B

A xor B

A xnor B

0

0

1

0

0

0

1

0

1

1

0

1

1

0

1

0

0

0

1

1

0

1

1

0

1

1

0

1

and from this we recognize “xnor” as “not xor.”

However, further playing with the calculator shows how the calculator does bitwise operations on floating point numbers. It discards fractional parts to operands, takes the values thus formed modulo 232, does the operation on the binary expansion of this, and finally treats these as signed twos complement values when storing them.

This is very similar to what JavaScript does. In fact, it is exactly what JavaScript does.

We decide to do the same thing in our routines. For the operands, we will discard fractional digits and take the value modulo 232. For the results (already integers between 0 and 232-1), we will wrap values of 231 or greater to negative values.

This time, we will recognize that we are in a programming tutorial, and so skip the problem analysis and go straight to the program analysis. (See, we actually can learn, sometimes.)

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

Bitwise Ops

Problem

Program

Code

Test

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