From 78ba60685c4677a3f04d44cdce77fda0cb419579 Mon Sep 17 00:00:00 2001 From: mfranz Date: Tue, 29 Jul 2008 17:17:09 +0000 Subject: [PATCH] some convenience functions for the built-in bits library --- Nasal/bits.nas | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Nasal/bits.nas diff --git a/Nasal/bits.nas b/Nasal/bits.nas new file mode 100644 index 000000000..4c5e9fe43 --- /dev/null +++ b/Nasal/bits.nas @@ -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; +} + +