Bitwise – SQL Server Syntax Example: Bitwise – T-SQL Example

Bitwise – SQL Server Syntax Example: Bitwise – T-SQL Example

Purpose: – Illustrates the for the .

Syntax:
operand1 bitwiseoperator operand2

– You can and (&), or(|), exclusively or (^) bits between two columns. 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.

Operator Meaning
& (Bitwise AND) Bitwise AND (two operands).
| (Bitwise OR) Bitwise OR (two operands).
^ (Bitwise Exclusive OR) Bitwise exclusive OR (two operands).

View Other SQL Server Syntax Examples

-- Bitwise example from http://idealprogrammer.com
-- PURPOSE: You can and (&), or(|), exclusively or (^) bits between two columns
--          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.
--   Here is a table of available combinations:
--Left operand  Right operand  
--binary        int, smallint, or tinyint
--bit           int, smallint, tinyint, or bit
--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
 
 
-- SYNTAX: 
-- Operator                Meaning  
-- & (Bitwise AND)          Bitwise AND (two operands).
-- | (Bitwise OR)           Bitwise OR (two operands).
-- ^ (Bitwise Exclusive OR) Bitwise exclusive OR (two operands).
-- 
 
 
CREATE TABLE bitwise
( 
a_int_value int NOT NULL,
b_int_value int NOT NULL
);
GO
INSERT bitwise VALUES (1, 2);
GO
 
--  (A | B)
--  0000 0000 0000 0001  = 1
--  0000 0000 0000 0010  = 2
--  -------------------
--  0000 0000 0000 0011  = 3
 
SELECT a_int_value | b_int_value AS 'Result of or-ing 1 & 2'
FROM bitwise;
GO
 
DROP TABLE bitwise;
GO

Sample Output for Bitwise Example

SQL Server Bitwise