How to flip a bit using the bitwise operator for int in TSQL?

I have a flags column in sql which is int.

I need to set a bit in a flag using an SQL statement. The corresponding C ++ operator looks something like this flags &= ~(unsigned long) (0x10000000).

I tried to do

update [Databases]
 set flags = flags & ~0x10000000

but i get

Operand data type varbinary is invalid for '~' operator.

Then I tried to see that the convert function would be yild

select flagInt = CONVERT(int , 0x10000000)

gives me 268435456.

select flagIntInvert = ~CONVERT(int , 0x10000000)

gives me -268435457

which matches the signed int

flag    268435456   int
        ~flag   -268435457  int

However i need unsigned versions

(uint32)flag    268435456   unsigned long
~(uint32)flag   4026531839  unsigned long

Is there a way to set the flag as inversion 0x10000000 ?

Thank.

+4
source share
1 answer

, NOT ~. Int SQL Server int.

, , , .

, 1, .

DECLARE @Value int = 1;

, -operator (|), :

SET @Value = @Value | 4;

5 int.

SELECT @Value;

, , , :

SELECT @Value & 4

, :

IF @Value & 4 = 4 ...

, .

+4

Source: https://habr.com/ru/post/1536358/


All Articles