Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

~ (Bitwise NOT) (Transact-SQL)

Performs a bitwise logical NOT operation on an integer value.

Topic link icon Transact-SQL Syntax Conventions

~ expression

Is any valid expression of any one of the data types of the integer data type category, the bit, or the binary or varbinary data types. expression is treated as a binary number for the bitwise operation.

Note Note


int if the input values are int.

smallint if the input values are smallint.

tinyint if the input values are tinyint.

bit if the input values are bit.

The ~ bitwise operator performs a bitwise logical NOT for the expression, taking each bit in turn. If expression has a value of 0, the bits in the result set are set to 1; otherwise, the bit in the result is cleared to a value of 0. In other words, ones are changed to zeros and zeros are changed to ones.

Important note Important

When you perform any kind of bitwise operation, the storage length of the expression used in the bitwise operation is important. We recommend that you use this same number of bytes when storing values. For example, storing the decimal value of 5 as a tinyint, smallint, or int produces a value stored with different numbers of bytes: tinyint stores data using 1 byte; smallint stores data using 2 bytes, and int stores data using 4 bytes. Therefore, performing a bitwise operation on an int decimal value can produce different results from those using a direct binary or hexadecimal translation, especially when the ~ (bitwise NOT) operator is used. The bitwise NOT operation may occur on a variable of a shorter length. In this case, when the shorter length is converted to a longer data type variable, the bits in the upper 8 bits may not be set to the expected value. We recommend that you convert the smaller data type variable to the larger data type, and then perform the NOT operation on the result.

The following example creates a table using the int data type to store the values and inserts the two values into one row.

USE tempdb;
    WHERE TABLE_NAME = 'bitwise')
DROP TABLE bitwise;
a_int_value int NOT NULL,
b_int_value int NOT NULL
INSERT bitwise VALUES (170, 75);

The following query performs the bitwise NOT on the a_int_value and b_int_value columns.

USE tempdb;
SELECT ~ a_int_value, ~ b_int_value
FROM bitwise;

Here is the result set:

--- --- 
-171  -76 

(1 row(s) affected)

The binary representation of 170 (a_int_value or A) is 0000 0000 1010 1010. Performing the bitwise NOT operation on this value produces the binary result 1111 1111 0101 0101, which is decimal -171. The binary representation for 75 is 0000 0000 0100 1011. Performing the bitwise NOT operation produces 1111 1111 1011 0100, which is decimal -76.

         0000 0000 1010 1010
         1111 1111 0101 0101
         0000 0000 0100 1011
         1111 1111 1011 0100
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.