Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Navigation

libslax bit Extension Library

The bit extension library contains functions that create and manipulate bit strings. The functions support 64-bit integer arguments. To incorporate functions from the bit extension library into SLAX scripts, include the namespace statement for that library in the script.

ns bit extension = "http://xml.libslax.org/bit";

Call the bit extension functions using the bit prefix and the function name. For example:

version 1.1 ;
ns bit extension = "http://xml.libslax.org/bit";

var $a = 63;
var $b = { expr "10111"; }

match / {
    <out> {
        <bit-and> {
            <a1> bit:and("101100", "100101");
            <a2> bit:and($a, $b);
            <a3> bit:and($a, number($b));
        }
        <bit-or> {
            <a1> bit:or("101100", "100101");
            <a2> bit:or($a, $b);
            <a3> bit:or($a, number($b));
        }
        <bit-mask> {
            <a1> bit:mask(0);
            <a2> bit:mask(8, 32);
        }
        <ops> {
            <a1> bit:to-int("10101");
        }
    }
}

Table 1 lists the functions available in the bit extension library and which are supported in SLAX 1.1 scripts.

Table 1: Functions in the bit Extension Library

Function and Arguments

Description

Example

bit:and(b1, b2)

Return the logical AND of two bit strings.

bit:and("101100", "100101")
return value: "100100"

bit:clear(b1,bitnum)

Set the specified bit in the bit string to zero and return the new bit string. Bits are numbered starting from zero. If the integer argument is greater than the bit string length, the bit string is extended.

bit:clear("11111", 0)
return value: "11110"

bit:clear("11111", 6)
return value: "0011111"

bit:compare(value1, value2)

Compare two values and return an integer less than, equal to, or greater than zero, if the first argument is found to be less than, equal to, or greater than the second argument, respectively.

bit:compare("10000", 16)
return value: 0

bit:compare("11111", "10000")
return value: 1

bit:from-hex(string, len?)

Return the value of the hex argument as a bit string. The optional second argument pads the bit string with leading 0’s until it is the specified length.

bit:from-hex("0x45", 8)
return value: "01000101"

bit:from-int(integer, len?)

Return the value of the integer argument as a bit string. The optional second argument pads the bit string with leading 0’s until it is the specified length.

bit:from-int(65,8)
return value: "01000001"

bit:mask(count, len?)

Return a bit string with count low-order bits set to one. The optional second argument pads the bit string with leading 0’s until it is the specified length.

bit:mask(4, 8)
return value: "00001111"

bit:nand(b1, b2)

Return the logical NAND of two bit strings.

bit:nand("101100", "100101")
return value: "010010"

bit:nor(b1, b2)

Return the logical NOR of two bit strings.

bit:nor("101100", "100101")
return value: "011011"

bit:not(b1)

Return the inversion (NOT) of a bit string.

bit:not("101100")
return value: "010011"

bit:or(b1, b2)

Return the logical OR of two bit strings.

bit:or("101100", "100101")
return value: "101101"

bit:set(b1, bitnum)

Set the specified bit in the bit string and return the new bit string. Bits are numbered starting from zero. If the integer argument is greater than the bit string length, the bit string is extended.

bit:set("1001", 2)
return value: "1101"

bit:set("1001", 6)
return value: "1001001"

bit:to-int(b1)

Return the value of the bit string argument as an integer.

bit:to-int("101100")
return value: 44

bit:to-hex(b1)

Return the value of the bit string argument as a string representation of the hex value.

bit:to-hex("101100")
return value: "0x2c"

bit:xor(b1, b2)

Return the logical XOR of two bit strings.

bit:xor("101100", "100101")
return value: "001001"

bit:xnor(b1, b2)

Return the logical XNOR of two bit strings.

bit:xnor("101100", "100101")
return value: "110110"

Published: 2013-07-26