PC(1) PC(1) NAME pc - programmer's calculator SYNOPSIS pc [ -n ] DESCRIPTION Pc is an arbitrary precision integer calculator with a spe- cial 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 expres- sions 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 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). Page 1 Plan 9 (printed 2/5/23) PC(1) PC(1) 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 high- est 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(a908,n908,...,a9N8,n9N8) Truncate each of the a9i8 arguments to n9i8 bits and concatenate their binary representation. 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 _ in all printed numbers, every n dig- its. < 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. ' 1 Enable numbering bits (disable with 0). If the base is Page 2 Plan 9 (printed 2/5/23) PC(1) PC(1) a power of two, print the number of the corresponding bit above each digit. SOURCE /sys/src/cmd/pc.y SEE ALSO bc(1), hoc(1) BUGS With the input base set to 16, terms such as ABC are ambigu- ous. They are interpreted as numbers only if there is no function or variable of the same name. To force interpreta- tion as a number, use the 0x prefix. Arbitrary bases should be supported, but are not supported by the mp(2) string functions. HISTORY Pc first appeared in 9front (August, 2016). Page 3 Plan 9 (printed 2/5/23)