pc – programmer’s calculator


pc [ -n ]


Pc is an arbitrary precision integer calculator with a special emphasis on supporting two’s complement bit operations and working with different number bases.

Pc reads input statements which are either expressions or control statements. Multiple statements in one line can be separated by semicolons. Pc prints the value of all expressions that are not terminated by a semicolon.

Pc can be run non-interactively by using the -n switch. In this case no input prompt is printed.

Expressions can use the C-like operators

+ - * ** (exponentiation)


/ % (Euclidean division, by default)


& | ^ ~ ! << >>


&& || (returning the second argument, if appropriate)


< >= < <= == !=


The $ operator performs sign extension. n$x truncates x to n bits and sign extends. If n is omitted, it is inferred from the highest set bit (the result is always ≤ 0 in this case).

Variables can be defined using =. The builtin variable @ always refers to the last printed result.

Numbers can use the prefixes 0b (binary), 0 (octal), 0d (decimal) and 0x (hexadecimal). _ in numbers can be added for readability and is ignored.

Builtin functions


Display n in binary.  


Display n in octal.  


Display n in decimal.  


Display n in hexadecimal.  

pb(n, b)

Display n in base b (currently must be one of 0, 2, 8, 10, 16; 0 uses the defined output base).  


Absolute value of n.  


n rounded to the nearest multiple of m. Numbers exactly halfway between are rounded to the next even multiple.  


n rounded down to the next multiple of m.  


n rounded up to the next multiple of m.  


n truncated to m bits.  


n truncated to m bits, with the highest bit interpreted as a sign bit.  


n truncated to m bits, with the order of bits reversed.  


The minimum number of bits required to represent n as an unsigned number.  


The minimum number of bits required to represent n as an signed number.  


The number of bits set in n.  


Truncate each of the ai arguments to ni bits and concatenate their binary representation.  


The greatest common divisor of n and m.  


The ceiling of the logarithm of a with respect to base b. b can be omitted, in which case it defaults to 2.  


The inverse of n mod m.  


A random number satisfying 0 ≤ rand(n) < n.  

Control statements

Control statements are always evaluated with default input base 10.

_ n

If n ≠ 0, insert _ in all printed numbers, every n digits.  

< n

Set the default input base to n (default 10). The input base can always be overriden by the base prefixes defined above.  

> n

Set the output base to n. If n = 0 (default), print each number in the base it was input in.  

/ 0

Use Euclidean division (default). a / b is rounded towards ±∞ (opposite sign as b). a % b is always non-negative.  

/ 1

Use truncating division (same as C). a / b is rounded towards zero. a % b can be negative.  

\fL\’ 1

Enable numbering bits (disable with 0). If the base is a power of two, print the number of the corresponding bit above each digit.  




bc(1), hoc(1)


With the input base set to 16, terms such as ABC are ambiguous. They are interpreted as numbers only if there is no function or variable of the same name. To force interpretation as a number, use the 0x prefix.

Arbitrary bases should be supported, but are not supported by the mp(2) string functions.


Pc first appeared in 9front (August, 2016).