KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How does the Bit Test operator work ?
PRODUCT: 4D Developer | VERSION: 11 | PLATFORM: Mac & Win
Published On: August 6, 2008

In 4D, Bitwise operators operate on Long integer expressions or values at the level of their individual bits (i.e. at the binary level).

While using the bitwise operators, you must think about a Long Integer value as an array of 32 bits. The bits are numbered from 0 to 31, from right to left.

For the Bit Test operation, the second operand indicates the number of the bit on which to act. Therefore, this second operand must be between 0 and 31 inclusive. Otherwise, the expression returns False for Bit Test.

Lets look at the example from our website: 0x0010000 ?? 16 = true

The number in our example is a hexadecimal number: 0x0010000

In decimal that number would be: 65536

In binary that number would be: 0001 0000 0000 0000 0000

Here is a table that illustrates the binary representation for this number:

Binary 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Decimal 65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1


The Bit Test operator:
Returns True if, in the first operand, the bit whose number
is indicated by the second operand is equal to 1.
Returns False if, in the first operand, the bit whose number
is indicated by the second operand is equal to 0.

Looking at the binary representation of that number, 0001 0000 0000 0000 0000, we number the bits counting up from 0 and starting from the right, working our way to the left. The 16th bit (counting up from 0) is 1, so the return value is true. If the 16th bit (counting up from 0) was 0, the return value would then be false.

For more information on Bitwise operators, please check the docs: Bitwise Operators