Like any craftsman, I have accumulated quite a few tools during my embedded software development career. Some of them proved to me more useful than others. And these generally useful tools ended up in my Embedded Toolbox. In this blog, I’d like to share some of my tools with you. Today, I’d like to start with my cross-platform Programmer’s Calculator called QCalc.
I’m sure that you already have your favorite calculator online or on your smartphone. But can your calculator accept complete expressions in the C-syntax, which you can cut-and-paste directly to and from your embedded code? How many buttons do you need to push to see your result in decimal, hex and binary? Well, QCalc can do this with less hassle than anything else I’ve seen out there. I begin with describing QCalc features and then I tell you how to download and launch it.
Expressions in C-Syntax
The most important feature of QCalc is that it accepts expressions in the C-syntax – with the same operands and precedence rules as in the C or C++ source code. Among others, the expressions can contain all bit-wise operators (<<, >>, |, &, ^, ~) as well as mixed decimal, hexadecimal and even binary constants. QCalc is also a powerful floating-point scientific calculator and supports all mathematical functions (sin(), cos(), tan(), exp(), ln(), …). Some examples of acceptable expressions are:
((0xBEEF << 16) | 1280) & ~0xFF – binary operators, mixed hex and decimal numbers
($1011 << 24) | (1280 >> 8) ^ 0xFFF0 – mixed binary, dec and hex numbers
(1234 % 55) + 4321/33 – remainder, integer division
pow(sin($pi),2) + pow(cos($pi),2) – scientific floating-point calculations, pi-constant
($0111 & $GPIO_EXTIPINSELL_EXTIPINSEL0_MASK) << ($GPIO_EXTIPINSELL_EXTIPINSEL1_SHIFT * 12) – (see user-defined variables)
NOTE: QCalc internally uses the Tcl command expr to evaluate the expressions. Please refer to the documentation of the Tcl expr command for more details of supported syntax and features.
Automatic conversion to hexadecimal and binary
If the result of expression evaluation is integer (as opposed to floating point), QCalc automatically displays the result in hexadecimal and binary formats (see QCalc GUI). For better readability the hex display shows a comma between the two 16-bit half-words (e.g.,
0xDEAD,BEEF). Similarly, the binary output shows a comma between the four 8-bit bytes (e.g.,
As the extension to the C-syntax, QCalc supports binary numbers in the range from 0-15 (0b0000-0b1111). These binary constants are represented as
$1111 and can be mixed into expressions. Here are a few examples of such expressions:
($0110 << 14) & 0xDEADBEEF
($0010 | $1000) * 123
History of inputs
QCalc remembers the history of up to 8 most recently entered expressions. You can recall and navigate the history of previously entered expressions by pressing the Up / Down keys.
The $ans variable
QCalc stores the result of the last computation in the $ans variable (note the dollar sign
$ in front of the variable name). Here are some examples of expressions with the
1/$ans – find the inverse of the last computation
log($ans)/log(2) – find log-base-2 of the last computation
QCalc allows you also to define any number of your own user variables. To set a variable, you simply type the expression
=alpha in the user input field. This will define the variable
alpha and assign it the value of the last computation (
$ans). Subsequently, you can use your
alpha variable in expressions by typing $
alpha (note the dollar sign
$ in front of the variable name). Here is example of defining and using variable
0xE000E000 – set some value into
=GPIO_BASE – define user variable
GPIO_BASE and set it to
$GPIO_BASE + 0x400 – use the variable
$GPIO_BASE in an expression
Note: The names of user variables are case-sensitive.
Expressions that you enter into QCalc might have all kinds of errors: syntax errors, computation errors (e.g., division by zero), undefined variable errors, etc. In all these cases, QCalc responds with the
Error message and the explanation of the error:
Downloading and Launching QCalc
QCalc is included in the open source QTools Collection, which you cad freely download from SourceForge or GitHub. Once you install QTools, QCalc is located in the sub-directory
qtools/bin/ and consists of a single file
qcalc.tcl. To launch QCalc, you need to open this file with the wish Tk interpreter.
NOTE: The wish Tk interpreter is included in the QTools Collection for Windows and is also pre-installed in most Linux distributions.
You use QCalc by typing (or pasting) an expression in the user input field and pressing the Enter key to evaluate the expression. You can conveniently edit any expression already inside the user input field, and you can recall the previous expressions by means of the Up/Down keys. You can also resize the QCalc window to see more or less of the input field.
QCalc on Windows
The wish Tk interpreter is conveniently provided in the same
qtools/bin/ directory as the
qcalc.tcl script. The directory contains also a shortcut
qcalc, which you can copy to your desktop.
QCalc on Linux
Most Linux distributions contain the Tk interpreter, which you can use to launch QCalc. You can do this either from a terminal, by typin
wish $QTOOLS/qcalc.tcl & or by creating a shortcut to
wish with the command-line argument
Wish.exe does not appear to be in QTools. Wish.exe should be in the QTOOLS\bin correct? https://github.com/QuantumLeaps/qtools/tree/master/bin
Wish.exe is included (obviously) only in the QTools collection for Windows (installer qtools_win32_.exe). It is not included in the QTools collection for POSIX/Linux (qtools_posix_.zip), so please make sure that you download and install the correct one. Again, QTools installers are available for download from: https://sourceforge.net/projects/qpc/files/QTools .
This looks like a great tool, though as far as I can tell though it’s not freely available. After a brief search for an alternative,
I’ve found SpeedCrunch (speedcrunch.org) which is excellent – perhaps even better than QCalc – and cross platform.
Seems to support all the same things, plus more.
great tool, especially the c expression tool that helps with complex bit wise operations
Great tool, simple and straight to the point as i like it. Many thanks for sharing.
Hi. I have been using it for years. Any plans for support 64bit word?
The new, Python version of QCalc now supports 64-bit arithmetic. Please see https://www.state-machine.com/new-version-of-qcalc-programmers-calculator/