简体   繁体   English

mysql / bitmask:选择NOT值

[英]mysql / bitmask: select for NOT value

I have a table with a bitmask column(unsigned int). 我有一个带有位掩码列的表(unsigned int)。 This field has values 1, 2, 4, 8, 16, etc. 该字段的值为1,2,4,8,16等。

How do I select for NOT a specific value? 如何选择NOT特定值? IE I don't care what the other bits are - only that a specific bit be 0. IE我不关心其他位是什么 - 只是特定位为0。

What I've tried: 我尝试过的:

select count(*) from mytable; 从mytable中选择count(*);

This gives me 3387255 . 这给了我3387255

select count(*) from mytable where outageMask & ~8; 从mytable中选择count(*),其中outageMask&〜8;

This gives me 552061 . 这给了我552061

So I would assume that: 所以我认为:

select count(*) from mytable where outageMask & 8; 从mytable中选择count(*),其中outageMask&8;

would give me 2835194 . 会给我2835194 Not so. 不是这样。 Instead I get 87711 . 相反,我得到了87711

What am I doing wrong? 我究竟做错了什么?

only that a specific bit be 0. 只有特定位为0。

This is important. 这个很重要。 Let's see, what you calculated (with 8 bit numbers for simplification): 让我们看看,你计算了什么(简化为8位数):

 8 = 0b00001000
~8 = 0b11110111

So, what do these bitmasks test if you BITWISE AND them with another value? 那么,如果你用另一个值BITWISE和它们,这些位掩码会测试什么? The first mask tests if bit "8" is set. 第一个掩码测试是否设置了位“8”。 The second mask tests if all other bits are set. 第二个掩码测试是否设置了所有其他位 But this is not what you intended, you wanted a negation of the whole statement. 但这不是你想要的,你想要否定整个陈述。 Which is easy: 这很容易:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM