Bitwise Operators

XYX&YX|YX^Y~X
000001
010111
100110
111100

See the following topics:

The 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 cannot be any one of the data types of the binary string data type category. The following table shows the supported operand data types.

Left operandRight operand
binaryintsmallint, or tinyint
bitintsmallinttinyint, or bit
bigintbigintintsmallinttinyintbinary, or varbinary
intintsmallinttinyintbinary, or varbinary
smallintintsmallinttinyintbinary, or varbinary
tinyintintsmallinttinyintbinary, or varbinary
varbinaryintsmallint, or tinyint

Examples:

SELECT BitValue, (BitValue & 1) FROM tblBitLookup WITH (NOLOCK)
SELECT BitValue, (BitValue & 8) FROM tblBitLookup WITH (NOLOCK)
SELECT BitValue, (BitValue & 9) FROM tblBitLookup WITH (NOLOCK)

Remove bit value from table…

UPDATE
	b
SET
	b.BitValue = b.BitValue - 8
FROM 
	dbo.tblBitTable b WITH (NOLOCK) 
WHERE 
	b.BitValue & 8 > 0

Sources:

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/bitwise-operators-transact-sql?view=sql-server-ver15

Last modified: January 7, 2020

Author

Comments

Write a Reply or Comment