# Embedded Toolbox: Programmer's Calculator

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.

### QCalc Features

#### 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., 0b11011110,10101101,10111110,11101111).

#### Binary constants

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 $0000$0001$0010,..., $1110, and $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 $ans variable:

1/$ans – find the inverse of the last computation log($ans)/log(2) – find log-base-2 of the last computation

#### User variables

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 $GPIO_BASE:

0xE000E000 – set some value into $ans =GPIO_BASE – define user variable GPIO_BASE and set it to $ans
$GPIO_BASE + 0x400 – use the variable $GPIO_BASE in an expression

Note: The names of user variables are case-sensitive.

#### Error handling

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:

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 $QTOOLS/qcalc.tcl.

[ - ]
Comment by June 28, 2017

Just cross-posted this blog on DSPRelated as well as I believe it could be of interest to DSP engineers.  Thanks for writing this Miro and publishing it here.

[ - ]
Comment by July 3, 2017

[ - ]
Comment by July 3, 2017

Thank you for pointing out the Reddit comments.

As anticipated in the blog post, people mostly offer their own favorite tools. However, the challenge still stands, because none of the suggested tools allows a developer to enter complete expressions in the C-syntax and obtain automatic conversion to hex and binary with less hassle than QCalc.

--MMS

[ - ]
Comment by July 3, 2017

I cant find the qcalc tool in the referenced locations on Github or Sourceforge, QPC_5.9.3 is what I downloaded from sourceforge. Could you check the links and update if nessecary?

[ - ]
Comment by July 3, 2017

Hi Pikhal,

--MMS

[ - ]
Comment by July 3, 2017

Thanks, I have got the correct package from your Sourceforge link. The Qtools link on Github dosent seem to have Qcalc though.

The calculator looks good.

[ - ]
Comment by September 14, 2017

Looks like a wonderful tool. Wish I could access it. Went to both links you gave PikHal and both contain the same images Neither of which contains QCalc or anything link wish in any directory.

[ - ]
Comment by July 6, 2017

Great tool. A must have for every embedded engineer. The UI is very minimal, yet offers maximum benefits. Thanks Miro Samek.

To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.