Function BAnd() Foundation

Calculates the result of a bitwise AND operation.

Syntax
BAnd( <nVa1>, <nVal2>, [<nVal3>,...] ) -> nBitwiseAnd
Parameters
<nVal1>
<nVal1> is a numeric expression that is used as the first operand for the bitwise AND operation.
<nVal2>
<nVal2> is a numeric expression that is used as the second operand. The parameter may be followed by an arbitrary number of additional operands.
Return

BAnd() returns the result of a bitwise AND operation between operands <nVal1> and <nVal2>. If additional parameters are passed, another bitwise AND operation is performed for each parameter. Once all parameters are processed, BAnd() returns the combined result of the AND operation.

Description

BAnd() is a numeric function that performs a bitwise AND operation between two or more operands. A bitwise AND switches on only those bits in the result which have corresponding bits set in all of the operands. The following table illustrates this correlation.

Results produced by a bitwise AND operation
Operand 1 Operand 2 Result
0 0 0
1 0 0
0 1 0
1 1 1

Bitwise operations are usually used with flag values, where a certain bit pattern represents a certain option being enabled or disabled. Using the function BAnd(), a flag value can be easily tested for the presence of a certain option.

Examples
BAnd()
// The example illustrates an exemplary usage of the function BAnd() 

#define OPTION_1     1 
#define OPTION_2     2 
#define OPTION_3     4 

#define DEFAULTS     OPTION_1 + OPTION_3 

PROCEDURE Main() 

 LOCAL nSetting := DEFAULTS 

   ? IsOptionSet( nSetting, OPTION_1 ) // Result: .T., option is set 
   ? IsOptionSet( nSetting, OPTION_2 ) // Result: .F., options is not set 

   WAIT "Done." 

RETURN 

// Check whether option 'nOption' is 
// set in the value passed 
FUNCTION IsOptionSet( nValue, nOption ) 
RETURN (BAnd(nValue, nOption) != 0) 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.