1
0
Fork 0

restore simple bits functions. Useful for setting/testing boolean values

inr MP transport, and for setting/clearing modifier bits in js config files.
This commit is contained in:
mfranz 2008-10-22 12:49:01 +00:00
parent 2f76461c8c
commit 6d5585ec65

37
Nasal/bits.nas Normal file
View file

@ -0,0 +1,37 @@
var _bits = [var _ = 1];
for (var i = 1; i < 32; i += 1)
append(_bits, _ += _);
# returns number <n> as bit string: bits.string(6) -> "110"
#
var string = func(n) {
if (!n)
return '0';
var s = "";
while (n) {
s = ((var v = int(n / 2)) + v != n) ~ s;
n = v;
}
return s;
}
# checks whether bit <b> is set in number <n>
#
var test = func(n, b) {
while (b) {
n /= 2;
b -= 1;
}
int(n) != int(n / 2) * 2;
}
# returns number <n> with bit <b> set
#
var set = func(n, b) test(n, b) ? n : n + _bits[b];
# returns number <n> with bit <b> cleared
#
var clear = func(n, b) test(n, b) ? n - _bits[b] : n;