john
john

Reputation: 2544

BigQuery: Xoring Elements of Two Arrays

I have a two arrays.

a = [1, 2, 3, 4]

b = [11, 22, 33, 44]

How can I xor the respective elements of two arrays to get a result as

result = [10, 20 ,34, 40] i-e 1^11 = 10, 2^22=20 and so on

I have tried BIT_XOR(x) but it takes one array and xor all of the elements of array.

SELECT BIT_XOR(x) AS bit_xor FROM UNNEST([1, 2, 3, 4]) AS x;

Thanks

Upvotes: 2

Views: 364

Answers (1)

Elliott Brossard
Elliott Brossard

Reputation: 33755

You can "zip" the two arrays together:

SELECT
  ARRAY(
    SELECT x ^ b[OFFSET(off)]
    FROM UNNEST(a) AS x WITH OFFSET off) AS bit_xor
FROM dataset.table

This combines the elements based on their offset in the two arrays.

Upvotes: 4

Related Questions