How do you turn off a specific bit in a bit mask?

11,704

Solution 1

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.

Solution 2

You could also use: MyBitmask &= ~128

Share:
11,704

Related videos on Youtube

Nikwin
Author by

Nikwin

I received a Computing and Software Systems (CSS) degree from the University of Washington in 1999 and have been working as a developer ever since. My focus has been on Microsoft development, in particular .Net (both WinForms and ASP.Net), but I also have experience in classic ASP and even a little COM. My current position is Senior Software Developer\Architect at TourFactory.com. We build a virtual tour product for real-estate agents.

Updated on January 31, 2020

Comments

  • Nikwin
    Nikwin over 4 years

    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?