Q:

Why does XORing two large binary numbers give a decimal output?

I am trying to XOR two large binary values. However,
echo ${hashArray[1]}
echo ${hashArray[2]}
echo $((${hashArray[1]} ^ ${hashArray[2]}))

gives:
10100100000111101011100001101110000110000100001000000111001001100010110000010010111101100110111001111100010010000000010101110111

00001110110000010110101101011011100101101000011100011101001101101010000100110001001110101101111100010001111010100011010000000100

4018181242766406943

Why does echo $((${hashArray[1]} ^ ${hashArray[2]})) output a decimal number? Shouldn't it be another large binary value?

A:

bash does have support for binary numbers, however your numbers are too big and will be truncated. Either do as mana suggests or split up the string or use a library that can handle arbitrary precision, e.g. perl's Math::BigInt comes to mind:
xor.pl
use Math::BigInt;

$x = Math::BigInt->new("0b" . $ARGV[0]); 
$y = Math::BigInt->new("0b" . $ARGV[1]);

print $x->bxor($y)->as_bin;

Assign the numbers to variables for convenience:
x=10100100000111101011100001101110000110000100001000000111001001100010110000010010111101100110111001111100010010000000010101110111
y=00001110110000010110101101011011100101101000011100011101001101101010000100110001001110101101111100010001111010100011010000000100

Run with:
perl xor.pl $x $y

Output:
0b10101010110111111101001100110101100011101100010100011010000100001000110100100011110011001011000101101101101000100011000101110011

A:

Do it "bitwise" like that:
#!/bin/bash

a="101"
b="011"

out=""
for ((i=0; i < ${#a}; i++ )); do
   out=${out}$((${a:$i:1} ^ ${b:$i:1}))
done

echo ${a} ^ ${b} = $out

output:
101 ^ 011 = 110

edit: The inputs need to have the same length!

