bash 为什么对两个大的二进制数进行异或会给出十进制输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12634503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:24:20  来源:igfitidea点击:

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

bash

提问by gp_xps

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?

为什么echo $((${hashArray[1]} ^ ${hashArray[2]}))输出一个十进制数?不应该是另一个大的二进制值吗?

回答by Thor

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

bash确实支持 binary numbers,但是您的数字太大并且会被截断。要么按照mana建议做,要么拆分字符串,要么使用可以处理任意精度的库,例如 perl 的 Math::BigInt 想到:

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

回答by mana

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!

编辑:输入需要具有相同的长度!

回答by V. Michel

Do bitwise with bc, firstly get the file logic.bc :

bc按位做,首先得到文件 logic.bc :

wget http://phodd.net/gnu-bc/code/logic.bc

Test xor()

测试异或()

x=10100100000111101011100001101110000110000100001000000111001001100010110000010010111101100110111001111100010010000000010101110111
y=00001110110000010110101101011011100101101000011100011101001101101010000100110001001110101101111100010001111010100011010000000100

echo "ibase=2;obase=2;xor($x,$y)" | bc -l logic.bc

Result:

结果:

10101010110111111101001100110101100011101100010100011010000100001000110100100011110011001011000101101101101000100011000101110011