In TSql, how do you turn off a specific bit in a bitmask without having to check to see if the bit is set or not?
+2
A:
Found it! Use & ~ like this...
UPDATE MyTable SET
MyBitmask = MyBitmask & ~128 -- 8th bit
WHERE MyID = 123
The ~ operator flips all the bits (1s become 0s and 0s become 1s). Just set the value that you flip to the one you want to turn off and use & to safely turn off just the one specific bit without having to check to see if the bit is set.
Brian
2010-04-26 23:43:37