The bitwise_not() Scalar Function : bitwise operators « Data Types « SQL Server / T-SQL Tutorial






5>
6> CREATE FUNCTION dbo.bitwise_not
7> (
8>   @arg1 varbinary(8)
9> ) RETURNS varbinary(8)
10> AS
11> BEGIN
12>   DECLARE
13>     @result   AS varbinary(8000),
14>     @numbytes AS int,
15>     @curpos   AS int
16>   SET @result = 0x
17>   SET @numbytes = DATALENGTH(@arg1)
18>   SET @curpos = 1
19>   WHILE @curpos <= @numbytes
20>   BEGIN
21>     SELECT
22>       @result = @result +
23>                 CAST(~ CAST(SUBSTRING(@arg1, @curpos, 1)
24>                 AS tinyint)AS binary(1))
25>     SET @curpos = @curpos + 1
26>   END
27>   RETURN @result
28> END
29> GO
1> GRANT EXECUTE ON dbo.bitwise_not TO public
2> GO
1> SELECT dbo.bitwise_not(0x00000001000000010000000100000001)
2>
3> drop function dbo.bitwise_not
4> GO





------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
----------------------------------
0xFFFFFFFEFFFFFFFE





(1 rows affected)








5.3.bitwise operators
5.3.1.Selecting data using bitwise operators.
5.3.2.Testing numeric values with bitwise operators.
5.3.3.Bitwise NOT (~) Validity Table
5.3.4.Legal Bitwise Operation
5.3.5.Retrieving Index Properties by Using the Bitwise AND (&) Operator
5.3.6.Bitwise AND (&)
5.3.7.Bitwise OR (|)
5.3.8.Exclusive Or (^)
5.3.9.Bitwise NOT (~)
5.3.10.The bitwise_not() Scalar Function
5.3.11.The bitwise_and() Scalar Function
5.3.12.The bitwise_or() Scalar Function
5.3.13.The bitwise_xor() Scalar Function