Start of change

BITAND, BITANDNOT, BITOR, BITXOR, and BITNOT

The bit manipulation functions operate on the twos complement representation of the integer value of the input arguments. The functions return the result as a corresponding base 10 integer value in a data type that is based on the data type of the input arguments.

>>-+-BITAND----+--(--expression1--,--expression2--)------------><
   +-BITANDNOT-+                                      
   +-BITOR-----+                                      
   '-BITXOR----'                                      

>>-BITNOT--(--expression--)------------------------------------><

The schema is SYSIBM.

Table 1. The bit manipulation funcitons
Function Description The bit in the twos complement representation of the result
BITAND Performs a bitwise AND operation. 1 - only if the corresponding bits in both arguments are 1.
BITANDNOT Clears any bit in the first argument that is in the second argument.
  • 0 - if the corresponding bit in the second argument is 1.
  • copied from the corresponding bit in the first argument - if the corresponding bit in the first argument is not 1.
BITOR Performs a bitwise OR operation. 1 - unless the corresponding bits in both arguments are 0.
BITXOR Performs a bitwise exclusive OR operation. 1 - unless the corresponding bits in both arguments are the same.
BITNOT Performs a bitwise NOT operation. Opposite of the corresponding bit in the argument.
expression, expression1, or expression2
expression, expression1, or expression2 must be integer values represented by the data types SMALLINT, INTEGER, BIGINT, or DECFLOAT. Arguments that are of type DECIMAL, REAL, or DOUBLE are cast to DECFLOAT. The value is truncated to a whole number.

The bit manipulation functions can operate on up to 16 bits for SMALLINT, 32 bits for INTEGER, 64 bits for BIGINT, and 113 bits for DECFLOAT. The range of supported DECFLOAT values includes integers from -2122 to 2122 - 1. Special values such as NaN or INFINITY are not supported.

If the two arguments have different data types, the argument that supports fewer bits is cast to a value with the data type of the argument that supports more bits. This cast impacts the bits that are set for negative values. For example, -1 as a SMALLINT value has 16 bits set to 1. When -1 is cast to an INTEGER value, it has 32 bits set to 1.

The result of the functions with two arguments has the data type of the argument that is highest in the data type precedence list for promotion. If either argument is DECFLOAT, the data type of the result is DECFLOAT(34).

The result of the BITNOT function has the same data type as the input argument, except that DECIMAL, REAL, DOUBLE, or DECFLOAT(16) returns DECFLOAT(34).

The result can be null; if any argument is null, the result is the null value.

Due to differences in internal representation between data types and on different hardware platforms, using functions (such as HEX) or host language constructs to view or compare internal representations of BIT function results and arguments is data type-dependent and not portable. The data type- and platform-independent way to view or compare BIT function results and arguments is to use the actual integer values.

The BITXOR function is can be used to toggle bits in a value.

The BITANDNOT function can be used to clear bits.

BITANDNOT(val, pattern) operates more efficiently than BITAND(val, BITNOT(pattern)).

The following examples are based on an ITEM table with a PROPERTIES column of type INTEGER.

Return all items for which the third property bit is set.
SELECT ITEMID FROM ITEM
  WHERE BITAND(PROPERTIES, 4) = 4;
Return all items for which the fourth or the sixth property bit is set.
SELECT ITEMID FROM ITEM
  WHERE BITAND(PROPERTIES, 40) <> 0;
Clear the twelfth property of the item whose ID is 3412.
UPDATE ITEM
  SET PROPERTIES = BITANDNOT(PROPERTIES, 2048)
  WHERE ITEMID = 3412;
Set the fifth property of the item whose ID is 3412.
UPDATE ITEM
  SET PROPERTIES = BITOR(PROPERTIES, 16)
  WHERE ITEMID = 3412;
Toggle the eleventh property of the item whose ID is 3412.
UPDATE ITEM
  SET PROPERTIES = BITXOR(PROPERTIES, 1024)
  WHERE ITEMID = 3412;
Switch all the bits in a 16-bit value that has only the second bit on.
SELECT  BITNOT(CAST(2 AS SMALLINT))
   FROM SYSIBM.SYSDUMMY1;
This example returns -3 (with a data type of SMALLINT).
End of change