diff options
author | aiju <devnull@localhost> | 2016-09-01 10:55:12 +0000 |
---|---|---|
committer | aiju <devnull@localhost> | 2016-09-01 10:55:12 +0000 |
commit | 30c9e34c0d0a0247b0adb2c343e30240ed0e2779 (patch) | |
tree | 51a51efcc952dd424c47090c85547ca9b5ca23b5 | |
parent | d552fed38514dc799c7fea95dfb632c8816c3f60 (diff) |
pc: add cat() function
-rw-r--r-- | sys/man/1/pc | 3 | ||||
-rw-r--r-- | sys/src/cmd/pc.y | 31 |
2 files changed, 34 insertions, 0 deletions
diff --git a/sys/man/1/pc b/sys/man/1/pc index 51ca88a1b..e4e11792b 100644 --- a/sys/man/1/pc +++ b/sys/man/1/pc @@ -93,6 +93,9 @@ The minimum number of bits required to represent \fIn\fR as an unsigned number. .I sbits(n) The minimum number of bits required to represent \fIn\fR as an signed number. .TP +.I cat(a0,n0,...,aN,nN) +Truncate each of the \fIa\fR arguments to \fIn\fR bits and concatenate their binary representation. +.TP .I gcd(n,m) The greatest common divisor of \fIn\fR and \fIm\fR. .TP diff --git a/sys/src/cmd/pc.y b/sys/src/cmd/pc.y index 0f456a6eb..b47cdd5f0 100644 --- a/sys/src/cmd/pc.y +++ b/sys/src/cmd/pc.y @@ -836,6 +836,36 @@ fnrev(int, Num **a) return a[0]; } +Num * +fncat(int n, Num **a) +{ + int i, w; + Num *r; + + if(n % 2 != 0){ + error("cat: odd number of arguments"); + i = 0; + fail: + for(; i < n; i++) + numdecref(a[i]); + return nil; + } + r = numalloc(); + for(i = 0; i < n; i += 2){ + if(toint(a[i+1], &w, 1)) goto fail; + mpleft(r, w, r); + if(a[i]->sign < 0 || mpsignif(a[i]) > w){ + a[i] = nummod(a[i]); + mptrunc(a[i], w, a[i]); + } + r->b = basemax(r->b, a[i]->b); + mpor(r, a[i], r); + numdecref(a[i]); + numdecref(a[i+1]); + } + return r; +} + void main(int argc, char **argv) { @@ -863,6 +893,7 @@ main(int argc, char **argv) regfunc("minv", fnminv, 2); regfunc("rand", fnrand, 1); regfunc("rev", fnrev, 2); + regfunc("cat", fncat, -1); prompt = 1; ARGBEGIN{ |