PC(1)PC(1)

## NAME

pc – programmer’s calculator

## SYNOPSIS

`pc``-n`

## DESCRIPTION

*Pc*

*Pc**Pc*

*Pc*`-n`

Expressions can use the C-like operators

`+ - * ** (exponentiation)`

`/ % (Euclidean division, by default)`

`& | ^ ~ ! << >>`

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

`< >= < <= == !=`

Variables can be defined using
`=`.`@`

Numbers can use the prefixes
`0b``0``0d``0x``_`

### Builtin functions

*bin(n)*

Display *n* in binary.

*oct(n)*

Display *n* in octal.

*dec(n)*

Display *n* in decimal.

*hex(n)*

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

*abs(n)*

Absolute value of *n*.

*round(n,m)*

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

*floor(n,m)*

*n* rounded down to the next multiple of *m*.

*ceil(n,m)*

*n* rounded up to the next multiple of *m*.

*trunc(n,m)*

*n* truncated to *m* bits.

*xtend(n,m)*

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

*rev(n,m)*

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

*ubits(n)*

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

*sbits(n)*

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

*nsa(n)*

The number of bits set in *n*.

*cat(a*

_{0},n_{0},...,a_{N},n_{N})Truncate each of the *a _{i}* arguments to

*n*bits and concatenate their binary representation.

_{i}*gcd(n,m)*

The greatest common divisor of *n* and *m*.

*clog(a,b)*

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

*minv(n,m)*

The inverse of *n* mod *m*.

*rand(n)*

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
`_`*n*

`<`

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

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

## SOURCE

`/sys/src/cmd/pc.y`

## SEE ALSO

## BUGS

With the input base set to 16, terms such as
`ABC``0x` prefix.

Arbitrary bases should be supported, but are not supported by the
*mp*(2)

## HISTORY

*Pc*