ref: 07284c41f6c4ca6b48e0a6ef389d867bdba2bb57
parent: 7bcbef11eb17d0b5f756acd74762d1a285aa134f
author: aiju <devnull@localhost>
date: Thu Sep 15 04:51:59 EDT 2016
pc(1): add nsa() command
--- a/sys/man/1/pc
+++ b/sys/man/1/pc
@@ -93,6 +93,9 @@
.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
--- a/sys/src/cmd/pc.y
+++ b/sys/src/cmd/pc.y
@@ -767,6 +767,28 @@
}
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 @@
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);