Mysql的位运算和滤波 [英] Mysql Bitwise operations and filter

查看:211
本文介绍了Mysql的位运算和滤波的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现使用MYSQL按位滤波器(UDF文件如果需要的话)

I try to implement a bitwise filter using MYSQL (with udf if needed)

过滤器有点像一个AND但我想用面膜来建立一个新的比特串...
让我用一个样本说明你:

The filter is something like a AND but I want to use the mask to build a new bit string... Let me explain you with a sample :

假设我有一滴存储8比特流的表格:

Suppose I have a table with blob storing 8 bit streams:


  • 数据1:10110110

  • 数据2:01100010

  • 数据3:00010011

然后我有一个面膜适用于从数据中得到位时,掩码值为1

Then I have a mask to apply to get the bits from data when mask value is 1


  • MASK:00101011

和因此得到以下预期成果:

And so get the following expected results:


  • 数据1:1010

  • 数据2:1010

  • 数据3:0011

有没有办法来优化筛选,不用循环的面具的每一位在数据行...

Is there a way to optimize the filtering, without looping on each bit of "mask" to get the corresponding value in "data" row...

澄清

我刚8位的职位,但它更像是256个字节

I've just taken 8 bits for the post, but it's more like 256 bytes

乔:为了澄清为例,面具00101011 PTED是作为跨$ P $:在3,5,7,8的位置获得数据域位的值,如果你读面具从左至右,枚举从第1位至第8位......希望这是澄清清除...

for Joe : To clarify the exemple, the mask 00101011 is interpreted as : get the bit value from data field at position 3,5,7,8, if you read the mask from left to right, enumerated from bit 1 to bit 8... Hope this clarification is "clear"...

推荐答案

我知道这样做的唯一方法你想要什么是一样的东西。

The only way I know of doing what you want is something like

SELECT ((data >> 2) & 8) | ((data >> 1) & 4) | (data & 3) FROM ...

显然,你必须根据你的面具,构建前pression;这不是很困难的事,只是有点乏味—你基本上需要循环位的面具,像这样(伪code):

Obviously, you'll have to construct the expression based on your mask; it's not very difficult to do, just a bit tedious — you basically need to loop over the bits in the mask, something like this (in pseudocode):

parts = new array;
shift = 0;
unshift = 0;
while (mask > 0) {
    while ((mask & 1) == 0) {
        shift = shift + 1;
        mask = mask >> 1;
    }
    submask = 0;
    while ((mask & 1) == 1) {
        submask = submask + (1 << unshift);
        unshift = unshift + 1;
        mask = mask >> 1;
    }
    parts.push( "((data >> " + shift + ") & " + submask + ")" );
}
expr = parts.join( " | " );

这篇关于Mysql的位运算和滤波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆