我需要在密码查询中按位进行“与”运算。似乎密码不支持按位运算。有其他建议吗?
这就是我要检测的...
例如268是(2 ^ 8 + 2 ^ 3 + 2 ^ 2),并且您可以看到2 ^ 3 = 8是我原始数字的一部分。因此,如果我按位使用,它将是(100001100)&(1000)= 1000,因此我可以检测8是否为268的一部分。
没有按位支持我该怎么办?有什么建议?我需要在密码中执行此操作。
最佳答案
使用cypher进行这种类型的测试的另一种方法是将十进制值转换为代表所设置的位的十进制集合。
// convert the binary number to a collection of decimal parts
// create an index the size of the number to convert
// create a collection of decimals that correspond to the bit locations
with '100001100' as number
, [1,2,4,8,16,32,64,128,256,512,1024,2048,4096] as decimals
with number
, range(length(number)-1,0,-1) as index
, decimals[0..length(number)] as decimals
// map the bits to decimal equivalents
unwind index as i
with number, i, (split(number,''))[i] as binary_placeholder, decimals[-i-1] as decimal_placeholder
// multiply the decimal value by the bits that are set
with collect(decimal_placeholder * toInt(binary_placeholder)) as decimal_placeholders
// filter out the zero values from the collection
with filter(d in decimal_placeholders where d > 0) as decimal_placeholders
return decimal_placeholders
这是返回值的示例。
然后,当您要测试数字是否为小数时,可以只测试集合中存在的实际小数。
with [4, 8, 256] as decimal_placeholders
, 8 as decimal_to_test
return
case
when decimal_to_test in decimal_placeholders then
toString(decimal_to_test) + ' value bit is set'
else
toString(decimal_to_test) + ' value bit is NOT set'
end as bit_set_test