I’m researching the rhythmic elements of prime number sequences in binary. I have multiple sets of files containing vertical lists, and I want to apply bitwise logic operators between any two of them line-by-line.
i.e.
$cat binary-superprimes.txt
11
101
1011
10001
11111
101001
111011
1000011
1010011
1101101
$cat binary-perniciousprimes.txt
11
101
111
1011
1101
10001
10011
11111
100101
101001
I’m looking for commands, a script, or an application (I’d prefer commands/a script but its not deal-breaking) that will let me and/or/xor/etc. these outputs in order line-by-line, in much the same style/similar to the output of diff
or comm
.
Using CentOS 7/Ubuntu 18.04/MacOS 10.15.
edit
Expected output (binary expansion of XORing each entry above in decimal):
0
0
1100
11010
10010
111000
101000
1011100
1110110
1000100
As for what I’ve tried, as I said I’ve played around with for loops, but I don’t know how (or if its possible) two iterate two lists for comparison in this context (i.e. two “for i in”‘s with a single “done” – using $i and $x as inputs for a basic “echo (($x^$i))”
I’ve also tried a program called “bitwise” but its output is too verbose and it cannot seem to read files, only values.
2
Answers
You can use
bc
for this purpose. First you create filexor.bc
Then you create loop to get the numbers one by one. And you can exec
XOR
by this:Assuming your
bash
version is >= 4.0 and supportsmapfile
,would you try the following:
Output:
In case your
bash
does not supportmapfile
command, please try the alternative:Hope this helps.