BitAnd


The BitAnd function returns the bit-wise AND of two numbers. Use the bit-wise AND to find the bits in common between two numbers.

Syntax

nResult = BitAnd( n1, n2 )

bullet.gif    nResult is the bit-wise AND of n1 and n2.

bullet.gif    n1 and n2 are two numbers (integers).

Remarks

The AND of two numbers saves the bits in common and discards those in one number but not the other. For example, consider the numbers 4 and 16 which, in binary notation are expressed 100 and 10000. The bit-wise AND gives 0 because there are no bits "on" at the same position in both numbers. However, 5 and 17 have a common bit in the 1's place (101 and 10001) so (5 AND 17) = 1,

The AND operation is often used to select one or more specific bits from a number. This is done by AND-ing the number with a "bit mask" made from the bits to be tested. For example, to test the "2" bit of a number, create a bit mask with value 10 binary, which is decimal 2. The result of (number AND 2) then gives 2 if the "2 bit" is "on", or it gives 0 if the 2 bit is "off". You also can use BitTest to determine whether a bit at a specified position is a "on" or "off" (i.e., 1 or 0).

The AND operation is often used to unpack a series of "bit flags" (i.e., 1, 2, 4, 8, 16, ... ) that were merged into a single number using the OR operation (see BitOr).

Example

The following script performs the AND of two numbers. Note that the result is printed using %u because it is considered an unsigned (i.e., 0 or positive) integer.

n1 = 7 n2 = 18

-- pick two numbers

Printf( "'%u'", BitAnd( n1, n2 ) )

-- prints the result 2

Related Topics

Boolean Math Functions

BitOr

BitTest

BitNand


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.