The bitwise operators operates on Long Integer expressions or values.
Note: If you pass an Integer or a Real value to a bitwise operator, 4D evaluates the value as a Long Integer value before calculating the expression that uses the bitwise operator.
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.
Because each bit can equal 0 or 1, you can also think about a Long Integer value as a value where you can store 32 Boolean values. A bit equal to 1 means True and a bit equal to 0 means False.
An expression that uses a bitwise operator returns a Long Integer value, except for the Bit Test operator, where the expression returns a Boolean value. The following table lists the bitwise operators and their syntax:
Operation | Operator | Syntax | Returns |
Bitwise AND | & | Long & Long | Long |
Bitwise OR (inclusive) | | | Long | Long | Long |
Bitwise OR (exclusive) | ^| | Long ^| Long | Long |
Left Bit Shift | << | Long << Long | Long | (see note 1) |
Right Bit Shift | >> | Long >> Long | Long | (see note 1) |
Bit Set | ?+ | Long ?+ Long | Long | (see note 2) |
Bit Clear | ?- | Long ?- Long | Long | (see note 2) |
Bit Test | ?? | Long ?? Long | Boolean | (see note 2) |
Notes
- For the Left Bit Shift and Right Bit Shift operations, the second operand indicates the number of positions by which the bits of the first operand will be shifted in the resulting value. Therefore, this second operand should be between 0 and 32. Note however, that shifting by 0 returns an unchanged value and shifting by more than 31 bits returns 0x00000000 because all the bits are lost. If you pass another value as second operand, the result is non significant.
- For the Bit Set, Bit Clear and Bit Test operations , the second operand indicates the number of the bit on which to act. Therefore, this second operand must be between 0 and 31. Otherwise, the expression returns the value of the first operand unchanged for Bit Set and Bit Clear, and returns False for Bit Test.
The following table lists the bitwise operators and their effects:
Operation | Description |
Bitwise AND | Each resulting bit is the logical AND of the bits in the two operands. |
| Here is the logical AND table: |
| 1 & 1 --> 1 |
| 0 & 1 --> 0 |
| 1 & 0 --> 0 |
| 0 & 0 --> 0 |
| In other words, the resulting bit is 1 if the two operand bits are 1;otherwise the resulting bit is 0. |
Bitwise OR (inclusive) | Each resulting bit is the logical OR of the bits in the two operands. |
| Here is the logical OR table: |
| 1 | 1 --> 1 |
| 0 | 1 --> 1 |
| 1 | 0 --> 1 |
| 0 | 0 --> 0 |
| In other words, the resulting bit is 1 if at least one of the two operand bits is 1; otherwise the resulting bit is 0. |
Bitwise OR (exclusive) | Each resulting bit is the logical XOR of the bits in the two operands. |
| Here is the logical XOR table: |
| 1 ^| 1 --> 0 |
| 0 ^| 1 --> 1 |
| 1 ^| 0 --> 1 |
| 0 ^| 0 --> 0 |
| In other words, the resulting bit is 1 if only one of the two operand bits is 1; otherwise the resulting bit is 0. |
Left Bit Shift | The resulting value is set to the first operand value, then the resulting bits are shifted to the left by the number of positions indicated by the second operand. The bits on the left are lost and the new bits on the right are set to 0. |
| Note: Taking into account only positive values, shifting to the left by N bits is the same as multiplying by 2^N. |
Right Bit Shift | The resulting value is set to the first operand value, then the resulting bits are shifted to the right by the number of position indicated by the second operand. The bits on the right are lost and the new bits on the left are set to 0. |
| Note: Taking into account only positive values, shifting to the right by N bits is the same as dividing by 2^N. |
Bit Set | The resulting value is set to the first operand value, then the resulting bit, whose number is indicated by the second operand, is set to 1. The other bits are left unchanged. |
Bit Clear | The resulting value is set to the first operand value, then the resulting bit, whose number is indicated by the second operand, is set to 0. The other bits are left unchanged. |
Bit Test | 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. |
1) The following table gives an example of each bit operator:
Operation | Example | Result |
Bitwise AND | 0x0000FFFF & 0xFF00FF00 | 0x0000FF00 |
Bitwise OR (inclusive) | 0x0000FFFF | 0xFF00FF00 | 0xFF00FFFF |
Bitwise OR (exclusive) | 0x0000FFFF ^| 0xFF00FF00 | 0xFF0000FF |
Left Bit Shift | 0x0000FFFF << 8 | 0x00FFFF00 |
Right Bit Shift | 0x0000FFFF >> 8 | 0x000000FF |
Bit Set | 0x00000000 ?+ 16 | 0x00010000 |
Bit Clear | 0x00010000 ?- 16 | 0x00000000 |
Bit Test | 0x00010000 ?? 16 | True |
2) 4D provides many predefined constants. The literals of some of these constants end with “bit” or “mask.” For example, this is the case of the constants provided in the
Resources Properties theme:
Constant |
Type |
Value |
Changed resource bit |
Longint |
1 |
Changed resource mask |
Longint |
2 |
Locked resource bit |
Longint |
4 |
Locked resource mask |
Longint |
16 |
Preloaded resource bit |
Longint |
2 |
Preloaded resource mask |
Longint |
4 |
Protected resource bit |
Longint |
3 |
Protected resource mask |
Longint |
8 |
Purgeable resource bit |
Longint |
5 |
Purgeable resource mask |
Longint |
32 |
System heap resource bit |
Longint |
6 |
System heap resource mask |
Longint |
64 |
These constants enable you to test the value returned by Get resource properties or to create the value passed to SET RESOURCE PROPERTIES. Constants whose literal ends with “bit” give the position of the bit you want to test, clear, or set. Constants whose literal ends with “mask” gives a long integer value where only the bit (that you want to test, clear, or set) is equal to one.
For example, to test whether a resource (whose properties have been obtained in the variable $vlResAttr) is purgeable or not, you can write:
If($vlResAttr ?? Purgeable resource bit)
or:
Conversely, you can use these constants to set the same bit. You can write:
or:
3) This example stores two Integer values into a Long Integer value. You can write:
$vlLong:=($viIntA<<16)|$viIntB
$vlIntA:=$vlLong>>16
$viIntB:=$vlLong & 0xFFFF
Tip: Be careful when manipulating Long Integer or Integer values with expressions that combine numeric and bitwise operators. The high bit (bit 31 for Long Integer, bit 15 for Integer) sets the sign of the value — positive if it is cleared, negative if it is set. Numeric operators use this bit for detecting the sign of a value, bitwise operators do not care about the meaning of this bit.