4D v13.4

Bitwise Operators

Home

 
4D v13.4
Bitwise Operators

Bitwise Operators  


 

 

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:

OperationOperatorSyntaxReturns
Bitwise AND&Long & LongLong
Bitwise OR (inclusive)|Long | LongLong
Bitwise OR (exclusive)^|Long ^| LongLong
Left Bit Shift<<Long << LongLong(see note 1)
Right Bit Shift>>Long >> LongLong(see note 1)
Bit Set?+Long ?+ LongLong(see note 2)
Bit Clear?-Long ?- LongLong(see note 2)
Bit Test??Long ?? LongBoolean(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:

OperationDescription
Bitwise ANDEach 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 ShiftThe 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 ShiftThe 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 SetThe 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 ClearThe 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 TestReturns 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.

Example  

1) The following table gives an example of each bit operator:

OperationExampleResult
Bitwise AND0x0000FFFF & 0xFF00FF000x0000FF00
Bitwise OR (inclusive)0x0000FFFF | 0xFF00FF000xFF00FFFF
Bitwise OR (exclusive)0x0000FFFF ^| 0xFF00FF000xFF0000FF
Left Bit Shift0x0000FFFF << 80x00FFFF00
Right Bit Shift0x0000FFFF >> 80x000000FF
Bit Set0x00000000 ?+ 160x00010000
Bit Clear0x00010000 ?- 160x00000000
Bit Test0x00010000 ?? 16True


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) ` 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.

 
PROPERTIES 

Product: 4D
Theme: Operators

 
SEE ALSO 

Comparison Operators
Date Operators
Logical Operators
Numeric Operators
Operators
Picture Operators
String Operators
Time Operators