Bit-flipping operations in T-SQL

前端 未结 2 610
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 11:15

I have a bitmasked int field in my database. Usually I manage it through C# code, but now I need to flip a bit in the mask using T-SQL

How do I accomplish the follow

相关标签:
2条回答
  • 2020-12-17 11:20

    TSql Bitwise operators can be found here and good article on how to use them is here

    0 讨论(0)
  • 2020-12-17 11:28

    Use XOR:

    SELECT value ^ 256
    

    So in your case, SELECT 143 ^ 256 will indeed return 399. If you want to pass in the exponent as well:

    SELECT value ^ POWER(2, power)
    
    0 讨论(0)
提交回复
热议问题