1
0
Fork 0

some convenience functions for the built-in bits library

This commit is contained in:
mfranz 2008-07-29 17:17:09 +00:00
parent 8e2fe030c6
commit 78ba60685c

39
Nasal/bits.nas Normal file
View file

@ -0,0 +1,39 @@
# test whether bit b (range 0..31) is set in integer number n
var test = func(n, b) {
var s = buf(4);
setfld(s, 0, 32, n);
fld(s, b, 1);
}
# return integer number n with bit b set
var set = func(n, b) {
var s = buf(4);
setfld(s, 0, 32, n);
setfld(s, b, 1, 1);
fld(s, 0, 32);
}
# return integer number n with bit b cleared
var clear = func(n, b) {
var s = buf(4);
setfld(s, 0, 32, n);
setfld(s, b, 1, 0);
fld(s, 0, 32);
}
# return number as bit string
var string = func(n) {
if (!n)
return "0";
var s = "";
while (n) {
s = ((var v = int(n / 2)) * 2 != n) ~ s;
n = v;
}
return s;
}