Bitwise operators (Transact-SQL)
Applies to: SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW)
Bitwise operators perform bit manipulations between two expressions of any of the data types of the integer data type category.
Bitwise operators convert two integer values to binary bits, perform the
NOT operation on each bit, producing a result. Then converts the result to an integer.
For example, the integer
170 converts to binary
75 converts to binary
If bits at any position are both
If either bit at any position is
Reverses the bit value at every bit position.
The following articles provide more information about the bitwise operators available in the Database Engine:
- & (Bitwise AND)
- &= (Bitwise AND Assignment)
- | (Bitwise OR)
- |= (Bitwise OR Assignment)
- ^ (Bitwise Exclusive OR)
- ^= (Bitwise Exclusive OR Assignment)
- ~ (Bitwise NOT)
The following bitwise operators were introduced in SQL Server 2022 (16.x):
Operands for bitwise operators can be any one of the data types of the integer or binary string data type categories (except for the image data type), except that both operands can't be any one of the data types of the binary string data type category. The following table shows the supported operand data types.
|Left operand||Right operand|
|binary||int, smallint, or tinyint|
|bit||int, smallint, tinyint, or bit|
|bigint||bigint, int, smallint, tinyint, binary, or varbinary|
|int||int, smallint, tinyint, binary, or varbinary|
|smallint||int, smallint, tinyint, binary, or varbinary|
|tinyint||int, smallint, tinyint, binary, or varbinary|
|varbinary||int, smallint, or tinyint|