Create a submasks table to make your job easier, add one row
z1 : z2 : z3
0xf : 0xf0 : 0xf00
Then use the following query
Select
t.*
from Table t
inner join submasks s
on (
((t.Mask1 & s.z1) = s.z1 || (t.Mask1 & s.z1) = (a00 & s.z1)) &&
((t.Mask1 & s.z2) = s.z2 || (t.Mask1 & s.z2) = (a00 & s.z2)) &&
((t.Mask1 & s.z2) = s.z2 || (t.Mask1 & s.z2) = (a00 & s.z2)) &&
((t.Mask2 & s.z1) = s.z1 || (t.Mask2 & s.z1) = (111 & s.z1)) &&
((t.Mask2 & s.z2) = s.z2 || (t.Mask2 & s.z2) = (111 & s.z2)) &&
((t.Mask2 & s.z2) = s.z2 || (t.Mask2 & s.z2) = (111 & s.z2)) &&
((t.Mask3 & s.z1) = s.z1 || (t.Mask3 & s.z1) = (12 & s.z1)) &&
((t.Mask3 & s.z2) = s.z2 || (t.Mask3 & s.z2) = (12 & s.z2))
)
The way this works is by comparing individual hex digits by performing a bitwise AND with z1
,z2
and z2
to get each of the 3 digits respectively.
so
<any value> & z1
sets all hex digits except the last to 0, ie0x123
becomes0x003
<any value> & z2
sets all hex digits except the second from last to 0, ie0x123
becomes0x020
<any value> & z3
sets all hex digits except the third from last to 0, ie0x123
becomes0x100
Using this filter the test for each digit can be built as
((mask & filter) = filter) // is the digit f
|| // OR
((mask & filter) = (test & filter)) // is the digit the same.
Repeat the test for each of z1
,z2
and z3
(ie 0x00f
, 0x0f0
, and 0xf00
) combine the results with an and condition and you can check all 3 hex digits of the mask are either f
or exactly the test value.
This is then repeated for Mask2 and Mask3 (but only z1,z2 as Mask3 is 2 digits).
By using inner join with the submasks table the result will only include the values from Table where the mask conditions are true.
UPDATE - you may want to perform select distinct
instead of just select
as if two masks match a single row in Table then 2 results will be returned.