summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authoraiju <devnull@localhost>2016-09-15 08:51:59 +0000
committeraiju <devnull@localhost>2016-09-15 08:51:59 +0000
commit07284c41f6c4ca6b48e0a6ef389d867bdba2bb57 (patch)
tree2012482b1f156216d8cb11308e8c9df874334b97 /sys
parent7bcbef11eb17d0b5f756acd74762d1a285aa134f (diff)
pc(1): add nsa() command
Diffstat (limited to 'sys')
-rw-r--r--sys/man/1/pc3
-rw-r--r--sys/src/cmd/pc.y23
2 files changed, 26 insertions, 0 deletions
diff --git a/sys/man/1/pc b/sys/man/1/pc
index 51444968f..90534e243 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 nsa(n)
+The number of bits set in \fIn\fR.
+.TP
.I cat(a\d\s70\s0\u,n\d\s70\s0\u,...,a\d\s7N\s0\u,n\d\s7N\s0\u)
Truncate each of the \fIa\d\s7i\s0\u\fR arguments to \fIn\d\s7i\s0\u\fR bits and concatenate their binary representation.
.TP
diff --git a/sys/src/cmd/pc.y b/sys/src/cmd/pc.y
index 910880ee7..331f35f35 100644
--- a/sys/src/cmd/pc.y
+++ b/sys/src/cmd/pc.y
@@ -767,6 +767,28 @@ fnsbits(int, Num **a)
}
Num *
+fnnsa(int, Num **a)
+{
+ int n, i;
+ mpdigit d;
+
+ a[0] = nummod(a[0]);
+ if(a[0]->sign < 0){
+ numdecref(a[0]);
+ return error("invalid argument");
+ }
+ n = 0;
+ for(i = 0; i < a[0]->top; i++){
+ d = a[0]->p[i];
+ for(; d != 0; d &= d-1)
+ n++;
+ }
+ itomp(n, a[0]);
+ a[0]->b = 0;
+ return a[0];
+}
+
+Num *
fngcd(int, Num **a)
{
a[0] = nummod(a[0]);
@@ -889,6 +911,7 @@ main(int argc, char **argv)
regfunc("xtend", fnxtend, 2);
regfunc("ubits", fnubits, 1);
regfunc("sbits", fnsbits, 1);
+ regfunc("nsa", fnnsa, 1);
regfunc("gcd", fngcd, 2);
regfunc("minv", fnminv, 2);
regfunc("rand", fnrand, 1);