These operators perform logical operations against integer type data.
Bit & operation - bits in the result are set to 1 if and only if both bits in the input expressions have a value of 1 bits in the result are set to 0 for any other combination
Bit | operation - bits in the result are set to 1 if either of the corresponding bits is a 1 bits in the result are set to 0 when both values are 0 in the corresponding bit
Bit ^ operation - bits in the result are set to 1 if 1 of the two bits (but not both) has a value of 1 bits in the result are set to 0 Any other combination (either all 0s or all 1s)
DECLARE @Status INT -- DECLARE VARIABLE OF INT
|
NOW YOU HAVE TWO BITS ARE ON i.e. 1 and 2
|
SELECT @Status AS 'SECOND BIT ON OR NOT'
WHERE (@Status & 2) = 2 -- CHECK THAT SECOND BIT IS ON OR NOT SET @Status = ((@Status) & ~2) -- RESET THE SECOND BIT SELECT @Status AS 'AFTER RESET SECOND BIT'-- SELECT THE VARIABLE SELECT @Status AS 'SECOND BIT ON OR NOT' WHERE (@Status & 2) = 2 -- CHECK THAT SECOND BIT IS ON OR NOT SET @Status = @Status |4 -- MAKE THIRD BIT ON [FOR THAT USE '|' (BITWISE OR)] SELECT @Status AS 'AFTER THIRD BIT ON' -- SELECT THE VARIABLE |
You can reset second bit and set third bit in a single sql statement
SET @Status = ((@Status) & ~2)|4 |

Akshay PatelPosted Aug 27, 2012, 1:03 AM
Hi Saurabh, Suppose you are working on Ecommerce web application. And you got a task to design product module. You need to maintain whether product is new, sold out, featured etc. Rather than keeping different bit field for new arrival, sold out, featured, you can maintain in a single field of int type. For example consider, 1st bit is for new arrival, second bit is for sold out and third bit is for featured. Now do bitwise operation on int field. If your product is new then set first bit on. If your product is sold out then set second bit on And in the case your product is not new and sold out, then set first bit off and second bit on. For these operations this query is useful.
saurabh bhattPosted Aug 26, 2012, 10:10 AM
Hello Akshay Patel I am newer to use sql server . I want Just where this Query useful?
saurabh bhattPosted Aug 26, 2012, 10:10 AM
Hello Akshay Patel I am newer to use sql server . I want Just where this Query useful?
saurabh bhattPosted Aug 26, 2012, 10:10 AM
Hello Akshay Patel I am newer to use sql server . I want Just where this Query useful?
Sandeep PrajapatiPosted Aug 6, 2012, 3:03 AM
Nice one & something new. Thanks a lot
Nachiket JorapurPosted Aug 3, 2012, 7:35 AM
very helpful thanks.
Digisha PancholiPosted Aug 3, 2012, 7:30 AM
Very Helpful,keep posting more..
Rajesh PatelPosted Aug 3, 2012, 6:59 AM
nice one akshay. keep more posting.