Mysql / bitmask: select NOT

I have a bitmask table (unsigned int). This field has values ​​1, 2, 4, 8, 16, etc.

How do I choose the NOT value for a specific value? IE I don't care what other bits are - just that a particular bit is 0.

What I have tried:

select count (*) from mytable;

This gives me 3387255 .

select count (*) from mytable where outageMask and ~ 8;

This gives me 552061 .

So I would suggest that:

select count (*) from mytable where outageMask and 8;

would give me 2835194 . Not this way. I get 87711 instead .

What am I doing wrong?

+3


source to share


1 answer


just that the particular bit is 0.

It is important. Let's see what you've calculated (with 8-bit numbers for simplicity):

 8 = 0b00001000
~8 = 0b11110111

      



So what do these bitmasks check if you BITWISE AND them with a different value? The first mask checks if bit "8" is set. The second mask checks if all other bits are set . But that is not what you intended, you need to deny the whole statement. It's easy:

select count(*) from mytable where (outageMask & 8) = 0;

      

+5


source







All Articles