2008-10-22 14:02:58 +00:00
|
|
|
var bit = [var _ = 1];
|
2008-10-22 12:49:01 +00:00
|
|
|
for (var i = 1; i < 32; i += 1)
|
2008-10-22 14:02:58 +00:00
|
|
|
append(bit, _ += _);
|
2008-10-22 12:49:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
}
|
|
|
|
|
2008-10-22 13:26:55 +00:00
|
|
|
|
2008-10-22 12:49:01 +00:00
|
|
|
# checks whether bit <b> is set in number <n>
|
|
|
|
var test = func(n, b) {
|
2008-10-22 14:02:58 +00:00
|
|
|
n /= bit[b];
|
|
|
|
return int(n) != int(n / 2) * 2;
|
2008-10-22 12:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# returns number <n> with bit <b> set
|
2008-10-22 14:02:58 +00:00
|
|
|
var set = func(n, b) test(n, b) ? n : n + bit[b];
|
2008-10-22 12:49:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# returns number <n> with bit <b> cleared
|
2008-10-22 14:02:58 +00:00
|
|
|
var clear = func(n, b) test(n, b) ? n - bit[b] : n;
|