QCalc Programmer's Calculator

**QCalc** is a powerful, cross-platform calculator specifically designed for embedded systems programmers. The calculator accepts *whole expressions* in the C-syntax and displays results simultaneously in decimal, hexadecimal, and binary without the need to explicitly convert the result to these bases.

- Note
- The calculator GUI provides a resizable
*user input*filed, where you can type**complete C expressions**. You can also**copy-and-paste**expressions into and from the*input filed*to use them directly in**your C code**.

**QCalc** is included in the QTools Collection 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 Enter 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 and Down keys. You can also resize the QCalc window to see more or less of the *input field*.

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.

Most Linux distributions contain the Tk interpreter, which you can use to launch QCalc. You can do this either from a terminal, by typing `wish $QTOOLS/qcalc.tcl &`

or by creating a shortcut to `wish`

with the command-line argument `$QTOOLS/qcalc.tcl`

.

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)`

- 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.

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`

).

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`

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.

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

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.

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: