The Programmer's Calculator
ROBERT M. BIRMINGHAM
When it comes to writing computer software, the old saying, "The better the tools, the easier the job" certainly rings true. When I'm programming, I often have to stop in the middle of whatever I'm doing and whip out the antiquated pencil and paper (remember those?), simply to solve some tiresome math problems. Not just your run-of-the-mill math problems, but countless conversions between number bases, ANDs, ORs, XORs and even (shudder) performing arithmetic operations with binary numbers. Having to do this day in and day out for several years is more than enough to push even the sanest person over the edge. Trust me, I know! Wouldn't it be nice if the computer would do all of this for you? I thought so. That's why I wrote the Programmer's Calculator.
The Programmer's Calculator is a GEM desk accessory that not only emulates a basic four-function calculator, but also takes the drudgery out of all the programmer's math you have to do. And perhaps most important, it saves paper!
To install the calculator, copy the file CALCULATOR.ACC to the same disk where you keep your other accessories; then reboot your computer. The Programmer's Calculator will now be accessible from the GEM desktop or from any program that supports drop-down menus.
The Basics of Operation
To activate the Programmer's Calculator, select the "Calculator..." menu item from the desk drop-down menu. The calculator will appear in a window, which you can move, top it and close, just as you would do with most other GEM windows. To press the calculator's keys, position the mouse cursor over the desired key, then click the left mouse button.
Since you're probably already familiar with how real calculators work, I won't describe how the Programmer's Calculator works in any great detail. A brief explanation of a few of the more interesting features is probably enough.
As with some other calculators, the two keys labeled CA and CE represent the Clear All and Clear Entry keys, respectively. The CA key will completely reset the calculator, erasing any operation you were performing. The CA key is also used to clear any errors when they occur. The CE key lets you take back the last operation you performed. This can be handy if you entered " + " when you meant " - " or entered the wrong number in an equation.
NOTE: Because of this program's large size, it is available only on this month's disk version or from the databases of the ST-LOG ST Users' Group on DELPHI.
Like all calculators, and even computers, there's a limit to the range of numbers the Programmer's Calculator can handle. For the decimal mode, the range is limited by the maximum of ten digits allowed for a number in this base ( + / -9,999,999,999). For the other number bases, the range is limited by the values that can fit into a 32-bit-long integer ( - 2,147,483,648 to + 2,147,483,647 .)
All the Bases Are Covered
One of the tasks often performed by the programmer is converting numbers from one base to another. The Programmer's Calculator supports the four most common number bases: decimal, hexadecimal, binary and octal. To convert a number from one base to another, select the base for the number you want to convert, enter your number, then select the number base you want to convert to. The converted number will appear in the display.
You may have noticed that when you switch to a different number base, some of the calculator's keys become enabled and some become disabled (when a key is disabled, you won't be able select it). This is because some keys apply to the current number base, while others don't. For example, the keys labeled "A" through "F" are enabled when you're in the hexadecimal number base, but are disabled for the decimal, binary and octal bases.
In addition to converting numbers, you may also perform arithmetic operations using any number base you like, just as you would with a normal calculator. For instance, you can add two binary numbers or even multiply two hexadecimal numbers. This can be a real time-saver, since most human beings are used to doing only decimal arithmetic.
Bit-Wise and Pound Foolish
To supplement the normal arithmetic operations of addition, subtraction, multiplication and division, there are also four bit-wise operations: AND, OR, XOR (eXclusive OR) and NOT. The bit-wise operations work in the same way as the arithmetic functions. So, if you wanted to OR two binary numbers together (for example, 1010 and 101), enter the first number, press the OR key, enter the second number, then press the equals key. The result will be 1111 (binary). Note that when performing a bit-wise operation in the decimal base, any fractional portion of the number in the display is discarded.
Shift to the Left, Shift to the Right!
Another common programmer's need is the ability to shift a number to the left or to the right, one bit at a time. For this, use the keys labeled " < < " (Shift Left) or "> >" (Shift Right). As with the bit-wise operations described above, if you're in the decimal mode, any fractional portion of the number in the display will be discarded before the shift is performed.
Clearing the Error
If you're in the decimal mode and you perform a calculation resulting in a number larger than the calculator can represent, an overflow error will occur. However, if you're in the hexadecimal, binary or octal modes, exceeding the maximum value the calculator can handle (a 32-bit integer) will cause the number to wrap around past zero. This really isn't an error, just something you should be aware of.
An error will also occur if you try to divide a number by zero. Unlike the over-flow error, this error can occur regardless of the number base you're in. Dividing by zero is an undefined operation in mathematics. To clear either an overflow error or a division-by-zero error, press the CA key to reset the calculator.
The Programmer's Calculator was written entirely in Megamax C. The methods used to emulate a calculator might not be readily apparent, so the source code is heavily commented. This should help you if you're interested in studying the code. You might also find some of the individual routines interesting. Feel free to yank them out and use them in your own programs.
I hope you find the Programmer's Calculator useful. It can be a handy tool whether you program in C, Assembly or any other language. If you have any questions, comments or criticisms, feel free to contact me on CompuServe. My user ID is 73637,1120. Enjoy!.
Robert Birmingham is 26 years old and lives in Miami. When he's not programming he enjoys cycling, model rocketry and juggling.