Loading...

Searching...

No Matches

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 is a console application, where you can type
**complete C expressions**. You can also**copy-and-paste**expressions into and from the calculator console to use them almost directly in**your C code**.

**QCalc** is included in the QTools Collection in the sub-directory qtools/qcalc/ and consists of a single file **qcalc.py**. To launch QCalc, you need to open this file with python.

You use QCalc by typing (or pasting) an expression at the `>`

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

- Note
- You
**quit**the calculator by Enter without entering an expression.

If you provide the optional [expression] argument, **QCalc** will evaluate the expression, print the result and terminate. For example:

qcalc "2*3 + (1 << 3)" QCalc Programmer's Calculator 6.9.4 running on Python 3.9.1 (c) 2005-2021 Quantum Leaps, www.state-machine.com 2*3 + (1 << 3) = 14 | 0x0000'000E | 0b00000000'00000000'00000000'00001110

Otherwise, if no [expression] argument is provided, **QCalc** will start in the interactive mode, where you can enter expressions via your keyboard.

The python interpreter is included in the QTools collection for Windows. The `QTOOLS%\bin`

directory also contains the `qcalc.bat`

batch file and a shortcut qcalc, which you can copy to your desktop:

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 (note the `//`

integer division operator

`pi/6`

—pi-constant

`pow(sin(and),2) + pow(cos(and),2)`

—scientific floating-point calculations, and-variable

- Note
- QCalc internally uses the Python command
**eval**to evaluate the expressions. Please refer to the documentation of Python math expressions 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 screenshot). For better readability, the hex display shows an apostrophe between the two 16-bit half-words (e.g., `0xDEAD'BEEF`

). Similarly, the binary output shows an apostrophe between the four 8-bit bytes (e.g., `0b11011110'10101101'10111110'11101111`

).

As an extension to the C-syntax, QCalc supports both **hexadecimal numbers** and **binary numbers**. These numbers are represented as `0x...`

and`0b...`

, respectively, and can be mixed into expressions. Here are a few examples of such expressions:

(0b0110011 << 14) & 0xDEADBEEF (0b0010 | 0b10000) * 123

As a console application QCalc "remembers" the history of the 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 `and`

variable. Here are some examples of expressions with the `and`

variable:

`1/and`

—find the inverse of the last computation

`log(and)/log(2)`

—find log-base-2 of the last computation

QCalc supports the 64-bit range and switches to 64-bit arithmetic automatically when an **integer** result of a computation exceeds the 32-bit range. Here are some examples of the 64-bit output:

> 0xDEADBEEF << 27 = 501427843159293952 | 0x06F5'6DF7'7800'0000 = 0b00000110'11110101'01101101'11110111'01111000'00000000'00000000'00000000 > 0xDEADBEEF << 24 = 62678480394911744 | 0x00DE'ADBE'EF00'0000 = 0b00000000'11011110'10101101'10111110'11101111'00000000'00000000'00000000 > 0xDEADBEEF << 34 ! out of range >

Expressions that you enter into QCalc might have all kinds of errors: syntax errors, computation errors (e.g., division by zero), etc. In each of these cases, QCalc responds with the `Error`

message and an explanation of the error:

> (2*4) + ) Traceback (most recent call last): File "C:\qp\qtools\qcalc\qcalc.py", line 54, in _main result = eval(expr) File "<string>", line 1 (2*4) + ) ^ SyntaxError: unmatched ')' >