4D v14.3Bitwise Operators |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v14.3
Bitwise Operators
|
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
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 |
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) ` 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.
Product: 4D
Theme: Operators
Comparison Operators
Date Operators
Logical Operators
Numeric Operators
Operators
Picture Operators
String Operators
Time Operators
4D Language Reference ( 4D v12.4)
4D Language Reference ( 4D v11 SQL Release 6)
4D Language Reference ( 4D v14 R3)
4D Language Reference ( 4D v14 R2)
4D Language Reference ( 4D v13.5)
4D Language Reference ( 4D v14.3)
4D Language Reference ( 4D v14 R4)