/
f0.noergaard.js
56 lines (50 loc) · 1.17 KB
/
f0.noergaard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ----------------------------------------------------------
// -- fredrik olofsson 010614 --
// -- ported from max c external 160524 --
// -- distributed under GNU GPL license --
// ----------------------------------------------------------
var valOut= 0;
inlets= 1;
outlets= 1;
setinletassist(0, "index >= 0 (int)");
setoutletassist(0, "value (int)");
if(jsarguments.length>1) {
post("warning: f0.noergaard "+(jsarguments.length-1)+" extra argument(s)");
}
function bang() {
outlet(0, valOut);
}
function msg_int(val) {
if(val>=0) {
valOut= theFunction(val);
bang();
} else {
post("f0.noergaard warning: only possitive indexes");
}
}
theFunction.local= 1;
function theFunction(index) {
var len, res= 0;
len= count_bits(index);
for(var i= len-1; i>=0; i--) {
if((index>>i)&1) {
res= res+1;
} else {
if(res>=0) {
res= 0-res;
} else {
res= Math.abs(res);
}
}
}
return res;
}
count_bits.local= 1;
function count_bits(n) {
var i= 0;
while(n!=0) {
n= n>>1;
i= i+1;
}
return i;
}