我需要在密碼查詢中按位「和」。似乎cypher不支持按位操作。任何建議的替代品? 這就是我想要檢測的... 例如,268是(2^8 + 2^3 + 2^2),正如你所看到的,2^3 = 8是我原始數字的一部分。所以,如果我使用按位與(100001100)&(1000)= 1000,那麼我可以檢測8是否是268的一部分。 如何在沒有按位支持的情況下執行此操作?有什麼建議麼?我需要在密碼中這樣做。Neo4j密碼查詢中的按位操作替代
1
A
回答
0
謝謝戴夫。我試過你的解決方案,他們都工作。對我來說,找到另一種方法是一個很好的暗示。這是我解決它的方式。我用字符串比較。
with '100001100' as number , '100000000' as sub_number
with number,sub_number,range(length (number)-1,length (number)-length(sub_number),-1) as tail,length (number)-length(sub_number) as difference
unwind tail as i
with i,sub_number,number, i - length (number) + length (sub_number) as sub_number_position
with sub_number_position, (split(number,''))[i-1] as bit_mask , (split(sub_number,''))[sub_number_position] as sub_bit
with collect(toInt(bit_mask) * toInt(sub_bit)) as result
return result
顯然,數字和子編號可以有不同的值。
0
幾乎可以肯定失敗選擇在第一時間按位操作的目的,但如果你絕對需要和兩個二進制數的暗號,你可以做這樣的事情與收藏。
with split('100001100', '') as bin_term_1
, split('000001000', '') as bin_term_2
, toString(1) as one
with bin_term_1, bin_term_2, one, range(0,size(bin_term_1)-1,1) as index
unwind index as i
with i, bin_term_1, bin_term_2, one,
case
when (bin_term_1[i] = one) and (bin_term_2[i] = one) then
1
else
0
end as r
return collect(r) as AND
1
如果你絕對必須做暗號的操作可能是一個更好的解決辦法是實現像@埃文的SO解決方案Alternative to bitwise operation使用暗號。
您可以通過使用暗號,看起來像這樣將您的數據開始...
// convert binary to a product of prime numbers
// start with the number to conver an a collection of primes
with '100001100' as number
, [2,3,5,7,13,17,19,23,29,31,37] as primes
// create an index based on the size of the binary number to convert
// take a slice of the prime array that is the size of the number to convert
with number
, range(length(number)-1,0,-1) as index
, primes[0..length(number)] as primes, decimals[0..length(number)] as decimals
// iterate over the index and match the prime number to the bits in the number to convert
unwind index as i
with (split(number,''))[i] as binary_place_holder, primes[-i-1] as prime_place_holder, decimals[-i-1] as decimal_place_holder
// collect the primes that are set by multiplying by the set bits
with collect(toInt(binary_place_holder) * prime_place_holder) as prime_placeholders
// filter out the zero bits
with filter(p in prime_placeholders where p > 0) as prime_placeholders
// return a product of primes of the set bits
return prime_placeholders, reduce(pp = 1, p in prime_placeholders | pp * p) as prime_product
樣品上面查詢的輸出。該查詢可以適用於使用主要產品更新屬性。
這裏是當你想使用它,你可以使用素數的模量的位置轉換如何分解
然後屏幕帽你想測試的位。
// test if the fourth bit is set in the decimal 268
// 268 is the equivalent of a prime product of 1015
// a modulus 7 == 0 will indicate the bit is set
with 1015 as prime_product
, [2,3,5,7,13,17,19,23,29,31,37] as primes
, 4 as bit_to_test
with bit_to_test
, prime_product
, primes[bit_to_test-1] as prime
, prime_product % primes[bit_to_test-1] as mod_remains
with
case when mod_remains = 0 then
'bit ' + toString(bit_to_test) + ' set'
else
'bit ' + toString(bit_to_test) + ' NOT set'
end as bit_set
return bit_set
2
使用密碼執行此類型測試的另一種方法是將您的十進制值轉換爲表示已設置位的小數的集合。
// 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
0
相關問題
- 1. Neo4j密碼查詢加速
- 2. neo4j密碼查詢優化
- 3. 慢neo4j密碼查詢
- 4. 加速neo4j密碼查詢
- 5. 結合Neo4j密碼查詢的最佳方法(UNION的替代方法)?
- 6. PHP的Neo4j密碼查詢生成器
- 7. 撤消最後的neo4j密碼查詢
- 8. 意外的neo4j密碼查詢結果
- 9. 在C源代碼中查找按位與操作
- 10. Neo4j密碼查詢已知路徑
- 11. neo4j密碼:如何查詢鏈表
- 12. java neo4j密碼查詢匹配節點
- 13. Neo4j密碼查詢性能低下
- 14. 不是運營商查詢密碼neo4j
- 15. 如何使用neo4j密碼查詢支持基於位置的查詢?
- 16. 按位或運算後按位與操作的查詢
- 17. 查找neo4j密碼查詢中所有節點的根節點
- 18. 使用bitset代替使用手寫的位操作代碼?
- 19. c - 解密中的位操作/操作
- 20. C編碼中的按位操作
- 21. 無法操作neo4j密碼中的路徑集合
- 22. Neo4j:從neo4j-shell運行密碼查詢時出現OutOfMemoryError
- 23. Neo4j和spring-data:數字索引密碼查詢不起作用?
- 24. 權限位操作查詢
- 25. 按位操作的操作
- 26. Neo4j檢查屬性密碼
- 27. Neo4j:Spring Data Neo4j中的Native Java API(或等效的密碼查詢)
- 28. 按位替代
- 29. 如何將複雜的密碼查詢更改爲neo4j中的簡單查詢?
- 30. 反彙編C代替操作代碼