Bit-flipping operations in T-SQL

一世执手 提交于 2019-12-18 04:33:32

问题


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 following:

The bit I want to flip: 1 << 8 (256)

The mask value before I flip: 143

The mask value after I flip: 399

This can be done without the bit operators that's missing in T-SQL, right?


回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/1110155/bit-flipping-operations-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!