## Bitwise Operators

version 6.0

The bitwise operators operates on Long Integer expressions or values.

Note: If you pass an Integer or a Real value to a bitwise operator, 4th Dimension 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

(1) 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.

(2) 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.

Examples

(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) 4th Dimension 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 System heap resource mask Long Integer 64 System heap resource bit Long Integer 6 Purgeable resource mask Long Integer 32 Purgeable resource bit Long Integer 5 Locked resource mask Long Integer 16 Locked resource bit Long Integer 4 Protected resource mask Long Integer 8 Protected resource bit Long Integer 3 Preloaded resource mask Long Integer 4 Preloaded resource bit Long Integer 2 Changed resource mask Long Integer 2 Changed resource bit Long Integer 1

```
```

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) ` Is the resource purgeable?
```

or:

```   If ((\$vlResAttr & Purgeable resource mask) # 0) Is the resource purgeable?
```

Conversely, you can use these constants to set the same bit. You can write:

```   \$vlResAttr:=\$vlResAttr ?+ Purgeable resource bit
```

or:

```   \$vlResAttr:=\$vlResAttr | Purgeable resource bit
```

(3) This example stores two Integer values into a Long Integer value. You can write:

```   \$vlLong:=(\$viIntA<<16) | \$viIntB ` Store two Integers in a Long Integer

\$vlIntA:=\$vlLong>>16 ` Extract back the integer stored in the high-word

\$viIntB:=\$vlLong & 0xFFFF ` Extract back the Integer stored in the low-word
```

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.