Contents | Index | < Browse | Browse >
usage: BITAND(string1 [,string2] [,pad])
default: bitand(string) = bitand(string,'20'x)

-the argument strings are logically ANDed together, with the length of the result being the longer of the two
-if a pad character is supplied, the shorter string is padded on the right. If not, the operation terminates at the end of the shorter string. The operation terminates at the end of the shorter string and the remainder of the longer string is appended to the result
-with only the 1st argument supplied, BITAND() supplies '20'x (ascii 32) as the default 2nd argument. This acts on the supplied string1 argument by replacing lower case characters with space characters, and replacing upper case characters by a NUL ('00'x characters). Example: say c2x(bitand('Test')) --> 00202020 (thanks to Stefan Haubenthal for this one)

The purpose of BITAND is string comparison. A couple of examples to illustrate (note the use of c2b() in combination with the BITAND() function to provide a printable result; without it you may not achieve a result that is viewable in the shell):

/* Example of BITAND() function */
a=c2b(d2c(1))
b=c2b(d2c(2))
say a
say b
say bitand(a,b)
EXIT

produces the following results that are easy to trace:

say a returns ----> 00000001
say b returns ----> 00000010
say bitand(a,b) returns ----> 00000000

In "a" the 1st (rightmost) bit is set and the remainder are not. In "b" the 2nd bit is set but the rest are not. When ANDing them, since none of the corresponding bits in both strings "a" and "b" are set, all prove false when ANDed together, therefore all bits are set as zeros (meaning "false", or "not true").

To illustrate further, in "a" the 1st bit is true, or "1", but bit 1 is false or zero in "b", therefore a-bit1 AND b-bit1 is "false" or "0"; that is they are both "not true" (or you could say: it is "false" that they are both true, therefore the answer is "0"). The same occurs for the rest of the bits when comparing strings "a" and "b" in bit-for-bit in sequence from right to left, least significant bit to most significant bit.

The manual gives the illustration of:

BITAND('0313'x,'FFF0'x) -- '0310'x

but to view the output in printable form you actually have to convert the result back to hex in this way:

say C2X(BITAND('0313'x,'FFF0'x)) displays --> 0310